How can I manage user config files in declative way in nixos without using home-manager
- User
How can I manage user config files in declative way in nixos without using home-manager
- Assistant
Managing user configuration files declaratively in NixOS without using
home-managercan 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.etcThe
environment.etcoption 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
systemdUser ServicesYou can manage user configurations and services by leveraging
systemduser services and timers.Examples
Example: Managing a
.bashrcfileLet's say you want to manage a user’s
.bashrcfile declaratively.-
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
yourusernamewith the actual username. -
Activate the configuration:
After adding this to your NixOS configuration file, apply the changes by running:
sudo nixos-rebuild switch
Example: Managing
.vimrcSimilarly, you can manage a
.vimrcfile declaratively.-
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" ]; }; } -
Activate the configuration:
sudo nixos-rebuild switch
Additional Tips
-
Access Environment Variables: You might use
${config.users.users.yourusername.home}to reference the home directory directly instead of hardcoding paths. -
Automate User Service Enabling: You can add
postBuildorpostActivationhooks 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. -