Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-05-13· OpenAIAi2 WildChat-4.8M (AI2)

How can I manage user config files in declative way in nixos without using home-manager

  1. User

    How can I manage user config files in declative way in nixos without using home-manager

  2. Assistant

    Managing user configuration files declaratively in NixOS without using home-manager can still be achieved by making use of Nix expressions and the NixOS configuration itself. Here's a basic approach for achieving this:

    1. Use environment.etc

    The environment.etc option in NixOS allows you to specify files that should be created in /etc. Although it is typically used for system-wide configuration files, you can creatively use it to manage user-specific config files by symlinking them to the appropriate places.

    2. Use systemd User Services

    You can manage user configurations and services by leveraging systemd user services and timers.

    Examples

    Example: Managing a .bashrc file

    Let's say you want to manage a user’s .bashrc file declaratively.

    1. Define the user configuration file as a Nix expression:

      In your configuration.nix:

      { config, pkgs, ... }:
      
      let
        userConfigDir = "/home/yourusername";
        bashrc = pkgs.writeText "bashrc" ''
          # Custom .bashrc content
          export PATH="$HOME/bin:$PATH"
          alias ll="ls -la"
        '';
      in {
        environment.etc = {
          ".bashrc".source = bashrc;
        };
      
        # Create a systemd service to copy the .bashrc file to the user's home directory
        systemd.user.services."setup-user-config" = {
          description = "Set up user configuration files";
          serviceConfig = {
            ExecStart = ''
              cp ${bashrc} ${userConfigDir}/.bashrc
              chown yourusername:yourusername ${userConfigDir}/.bashrc
            '';
            Type = "oneshot";
            RemainAfterExit = true;
          };
          wantedBy = [ "default.target" ];
        };
      }
      

      Replace yourusername with the actual username.

    2. Activate the configuration:

      After adding this to your NixOS configuration file, apply the changes by running:

      sudo nixos-rebuild switch
      

    Example: Managing .vimrc

    Similarly, you can manage a .vimrc file declaratively.

    1. Define the Vim configuration:

      { config, pkgs, ... }:
      
      let
        userConfigDir = "/home/yourusername";
        vimrc = pkgs.writeText "vimrc" ''
          " Custom .vimrc content
          set number
          syntax on
        '';
      in {
        environment.etc = {
          ".vimrc".source = vimrc;
        };
      
        systemd.user.services."setup-user-config" = {
          description = "Set up user configuration files";
          serviceConfig = {
            ExecStart = ''
              cp ${vimrc} ${userConfigDir}/.vimrc
              chown yourusername:yourusername ${userConfigDir}/.vimrc
            '';
            Type = "oneshot";
            RemainAfterExit = true;
          };
          wantedBy = [ "default.target" ];
        };
      }
      
    2. Activate the configuration:

      sudo nixos-rebuild switch
      

    Additional Tips

    1. Access Environment Variables: You might use ${config.users.users.yourusername.home} to reference the home directory directly instead of hardcoding paths.

    2. Automate User Service Enabling: You can add postBuild or postActivation hooks to ensure user services are enabled and started.

    systemd.user.services."setup-user-config" = {
      description = "Set up user configuration files";
      serviceConfig = {
        ExecStart = ''
          cp ${bashrc} ${userConfigDir}/.bashrc
          chown yourusername:yourusername ${userConfigDir}/.bashrc
        '';
        Type = "oneshot";
        RemainAfterExit = true;
      };
      wantedBy = [ "default.target" ];
      ensure = "started";
    };
    

    By following these steps, you can manage user configuration files in a declarative manner directly within your NixOS configuration without requiring additional tools like home-manager.