- Add comprehensive Home Manager refactoring plan document - Create initial Home Manager directory structure with user configs - Add modular Emacs configuration for Home Manager integration - Update system configurations for Home Manager compatibility - Preserve existing functionality while preparing for migration 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
15 KiB
Home Manager Refactoring Plan
Executive Summary
This document outlines a comprehensive plan to refactor the current user configuration management system from a system-centric NixOS approach to a user-centric Home Manager approach. The refactoring will provide better separation of concerns, improved dotfile management, user-level service management, and enhanced maintainability while preserving the existing sophisticated Emacs configuration system.
Current Architecture Analysis
Strengths of Current System
- Declarative Configuration: All configurations defined in Nix modules
- Sophisticated Emacs Integration: Profile-based Emacs system with Nix tool integration
- Multi-machine Consistency: Centralized configuration repository
- Modular Design: Clear separation between machines, modules, and users
- Nix Tool Integration: Excellent integration with Nix-provided tools and language servers
Current Limitations
- System-level User Configuration: User preferences mixed with system configuration
- No User Service Management: Cannot manage user-level systemd services
- Manual Dotfile Deployment: Limited systematic dotfile management
- Monolithic User Modules: Large, complex user configuration files
- No Per-user Package Management: User packages defined at system level
Refactoring Goals
Primary Objectives
- Separate User from System Configuration: Move user-specific settings to Home Manager
- Enable User Service Management: Leverage Home Manager's systemd user service support
- Systematic Dotfile Management: Implement proper dotfile deployment and management
- Preserve Emacs Excellence: Maintain current sophisticated Emacs configuration
- Improve Maintainability: Reduce complexity and improve modularity
Secondary Objectives
- Multi-user Support: Better support for multiple users with different preferences
- Development Environment Isolation: User-level development environments
- Profile-based Configuration: Different user profiles for different contexts
- Improved Package Management: User-level package installation and management
Migration Strategy
Phase 1: Foundation Setup
Timeline: Week 1-2 Risk Level: Low
Tasks:
-
Initialize Home Manager Integration
- Add Home Manager to flake.nix inputs
- Configure basic Home Manager module in NixOS
- Create initial home.nix for user 'geir'
-
Create Home Manager Directory Structure
home-manager/ ├── users/ │ ├── geir/ │ │ ├── home.nix # Main user configuration │ │ ├── profiles/ │ │ │ ├── development.nix # Development profile │ │ │ ├── workstation.nix # Full workstation profile │ │ │ └── minimal.nix # Minimal profile │ │ ├── programs/ │ │ │ ├── emacs.nix # Emacs configuration │ │ │ ├── zsh.nix # Shell configuration │ │ │ ├── git.nix # Git configuration │ │ │ └── development.nix # Development tools │ │ └── services/ │ │ ├── emacs-daemon.nix # User-level Emacs daemon │ │ └── desktop.nix # Desktop services │ └── sma/ │ └── home.nix # Admin user configuration ├── modules/ │ ├── emacs/ # Shared Emacs modules │ ├── development/ # Development environment modules │ └── desktop/ # Desktop environment modules └── dotfiles/ ├── emacs/ # Emacs configuration files ├── shell/ # Shell configuration files └── desktop/ # Desktop configuration files
-
Basic User Configuration Migration
- Migrate user packages from system to Home Manager
- Set up basic shell configuration
- Implement minimal dotfile management
Deliverables:
- Working Home Manager integration
- Basic user configuration in Home Manager
- Preserved system functionality
Phase 2: Emacs Migration
Timeline: Week 3-4 Risk Level: Medium
Tasks:
-
Migrate Emacs Configuration to Home Manager
- Preserve current profile-based system
- Migrate from
/etc/emacs/
to Home Manager file management - Maintain Nix tool integration
- Convert systemd service to user service
-
Enhanced Emacs Module Structure
programs.emacs = { enable = true; profile = "development"; # or "workstation", "minimal" package = emacsWithProfile; profiles = { minimal = { packages = [...]; extraConfig = "..."; }; development = { packages = [...]; extraConfig = "..."; }; workstation = { packages = [...]; extraConfig = "..."; }; }; nixIntegration = { enableLspServers = true; enableFormatters = true; tools = { ripgrep = true; fd = true; sqlite = true; }; }; };
-
Dotfile Management for Emacs
- Use
xdg.configFile
for Emacs configuration deployment - Maintain modular structure
- Preserve profile-based loading
- Use
Deliverables:
- Emacs fully managed by Home Manager
- User-level Emacs daemon service
- Preserved profile-based functionality
- Improved dotfile deployment
Phase 3: Complete User Migration
Timeline: Week 5-6 Risk Level: Medium
Tasks:
-
Shell Configuration Migration
- Move zsh configuration to Home Manager
- Implement user-specific aliases and functions
- Preserve current shell enhancement integration
-
Development Environment Setup
- User-level development tools
- Language server configurations
- Project-specific environments
-
Desktop Environment Integration
- User-level desktop services
- Application-specific configurations
- Theme and appearance management
Deliverables:
- Complete user environment in Home Manager
- System packages reduced to essential only
- User-specific development environments
Phase 4: Multi-user and Profiles
Timeline: Week 7-8 Risk Level: Low
Tasks:
-
Multi-user Support
- Migrate 'sma' user configuration
- Shared modules for common functionality
- User-specific customizations
-
Profile System Enhancement
- Machine-specific user profiles
- Context-aware configurations
- Easy profile switching
-
Documentation and Testing
- Comprehensive documentation
- Migration testing on different machines
- Rollback procedures
Deliverables:
- Complete multi-user Home Manager setup
- Profile-based user configurations
- Comprehensive documentation
Technical Implementation Details
Home Manager Integration Pattern
# flake.nix
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
home-manager = {
url = "github:nix-community/home-manager";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { nixpkgs, home-manager, ... }: {
nixosConfigurations = {
little-rascal = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
./machines/little-rascal/configuration.nix
home-manager.nixosModules.home-manager
{
home-manager.useGlobalPkgs = true;
home-manager.useUserPackages = true;
home-manager.users.geir = import ./home-manager/users/geir/home.nix;
home-manager.users.sma = import ./home-manager/users/sma/home.nix;
}
];
};
};
};
}
Emacs Configuration Structure
# home-manager/users/geir/programs/emacs.nix
{ config, lib, pkgs, ... }:
let
emacsProfile = config.programs.emacs.profile or "development";
# Package sets remain the same as current implementation
packageSets = {
essential = epkgs: with epkgs; [ use-package diminish bind-key which-key ];
minimal = epkgs: with epkgs; [ /* minimal packages */ ];
development = epkgs: with epkgs; [ /* development packages */ ];
workstation = epkgs: with epkgs; [ /* full packages */ ];
};
emacsWithProfile = profile:
(if profile == "nox" then pkgs.emacs-nox else pkgs.emacs-pgtk).pkgs.withPackages
(epkgs: packageSets.essential epkgs ++
(if profile == "nox"
then packageSets.minimal epkgs
else packageSets.development epkgs ++ packageSets.workstation epkgs));
in {
options.programs.emacs.profile = lib.mkOption {
type = lib.types.enum [ "gui" "nox" ];
default = "gui";
};
config = {
programs.emacs = {
enable = true;
package = emacsWithProfile emacsProfile;
};
services.emacs = {
enable = true;
package = config.programs.emacs.package;
};
xdg.configFile = {
"emacs/init.el".source = ../../../dotfiles/emacs/init-nix.el;
"emacs/modules/ui.el".source = ../../../dotfiles/emacs/modules/ui.el;
"emacs/modules/completion.el".source = ../../../dotfiles/emacs/modules/completion.el;
# ... other modules
};
home.sessionVariables = {
EMACS_PROFILE = emacsProfile;
RG_PATH = "${pkgs.ripgrep}/bin/rg";
FD_PATH = "${pkgs.fd}/bin/fd";
# ... other tool paths
};
};
}
Dotfile Management Strategy
# home-manager/users/geir/home.nix
{
# XDG-compliant configuration files
xdg.configFile = {
"niri/config.kdl".source = ../../dotfiles/niri/config.kdl;
"git/config".source = ../../dotfiles/git/config;
};
# Home directory files
home.file = {
".zshrc".source = ../../dotfiles/shell/zshrc;
".tmux.conf".source = ../../dotfiles/tmux/tmux.conf;
};
# Direct content management
xdg.configFile."starship.toml".text = ''
# Starship configuration
format = "$all$character"
'';
}
Risk Assessment and Mitigation
High-Risk Areas
-
Emacs Configuration Migration
- Risk: Breaking existing sophisticated Emacs setup
- Mitigation:
- Preserve exact current functionality first
- Test on non-critical machine first
- Maintain parallel system during transition
-
User Service Management
- Risk: Service startup/shutdown issues
- Mitigation:
- Test user services thoroughly
- Provide fallback to system services
- Document service debugging procedures
Medium-Risk Areas
-
Package Dependency Changes
- Risk: Missing packages or version conflicts
- Mitigation:
- Careful package migration tracking
- Test on all machine types
- Maintain system packages temporarily
-
Dotfile Deployment
- Risk: Configuration file conflicts or missing files
- Mitigation:
- Backup existing configurations
- Gradual migration approach
- Collision detection and resolution
Low-Risk Areas
-
Shell Configuration
- Risk: Minor shell behavior changes
- Mitigation: Preserve exact current shell configuration
-
Git Configuration
- Risk: Git workflow disruption
- Mitigation: Migrate settings exactly as-is
Testing Strategy
Phase Testing
- Per-phase Testing: Each phase tested independently
- Machine-specific Testing: Test on each machine type
- User-specific Testing: Test for both geir and sma users
- Rollback Testing: Verify rollback procedures work
Test Environments
- Development VM: Safe testing environment
- Non-critical Machine: Real hardware testing
- Critical Machines: Final validation
Test Cases
- Emacs Functionality: All profiles, modules, and integrations
- Development Workflow: Complete development environment
- Service Management: User services start/stop correctly
- Multi-machine: Configuration works across all machines
- Package Management: All user packages available
Migration Timeline
Week | Phase | Focus | Risk Level |
---|---|---|---|
1-2 | Foundation | Home Manager setup, basic migration | Low |
3-4 | Emacs | Emacs configuration migration | Medium |
5-6 | Complete User | Full user environment migration | Medium |
7-8 | Polish | Multi-user, profiles, documentation | Low |
Success Criteria
Functional Requirements
- All current functionality preserved
- Emacs profiles work exactly as before
- User services managed by Home Manager
- Dotfiles systematically deployed
- Development environment fully functional
Quality Requirements
- Configuration is more modular and maintainable
- Clear separation between user and system configuration
- Comprehensive documentation
- Easy rollback capability
- Multi-user support functional
Performance Requirements
- No degradation in system startup time
- Emacs startup time maintained or improved
- Home Manager switch time acceptable (<30 seconds)
Rollback Plan
Emergency Rollback
- Git Revert: Revert to pre-migration commit
- System Rebuild:
sudo nixos-rebuild switch --flake .
- Service Restart: Restart affected services
Partial Rollback
- Disable Home Manager: Comment out Home Manager in machine configuration
- Restore System Packages: Move packages back to system level
- Manual Service Management: Manually manage services during transition
Validation Steps
- Emacs Functionality: Verify Emacs starts and works correctly
- Development Environment: Verify development tools work
- User Services: Verify essential services are running
- System Stability: Verify system is stable and responsive
Documentation Deliverables
User Documentation
- Migration Guide: Step-by-step migration instructions
- Home Manager Usage: How to use Home Manager for daily workflow
- Profile Management: How to switch and manage user profiles
- Troubleshooting Guide: Common issues and solutions
Developer Documentation
- Architecture Documentation: New system architecture
- Module Documentation: How modules are structured and work
- Extension Guide: How to add new functionality
- Testing Procedures: How to test changes
Conclusion
This refactoring plan provides a comprehensive roadmap for migrating from the current system-centric user configuration to a modern Home Manager-based approach. The phased approach minimizes risk while ensuring all current functionality is preserved and enhanced. The end result will be a more maintainable, flexible, and user-centric configuration management system that better separates concerns between system and user configuration.
The plan prioritizes preserving the excellent current Emacs configuration while adding the benefits of proper user-level service management, systematic dotfile deployment, and improved modularity. The migration can be executed with confidence due to the comprehensive testing strategy and rollback procedures.