TIL: Symlinking NixOS Dotfiles

ibobev1 pts0 comments

TIL: Symlinking NixOS Dotfiles

The standard answer to managing dotfiles on NixOS is<br>home-manager. I’ve never used it, due to two aesthetic and one<br>practical objection:

I avoid dependencies, especially in nix, which rivals Python in the<br>number of approaches to dependency management.

home-manager installs packages for the current user only, which<br>makes sense on non-NixOS systems. But on a single-user desktop<br>system, I prefer having just one set of packages.

Having a source of truth for dotfiles be in nix store requires<br>rebuilding your system to change config, which gets in the way of<br>Emacs-style direct tinkering.

The approach I like is storing dotfiles in the same repository as<br>flake.nix / configuration.nix<br>and symlinking them in place.

The problem here is that NixOS seemingly doesn’t have a “native” way<br>to say that /a/b/c should be a symlink to /c/d/e. Or has it?

If you search options for symlink, you’ll learn about environment.etc which allows you to configure<br>symlinks, but only for things in /etc, not your ~/.config.

For the latter, you can use gnu stow or some other dotfile link manager, but the complexity<br>of the problem of just managing symlinks doesn’t warrant yet<br>another dependency. It’s fine to do<br>this manually.

But wouldn’t it be nice if this framework for declarative<br>configuration of your system allowed you to declaratively configure<br>symlinks? Turns out this is possible, in roundabout way. Inaptly-named<br>systemd-tmpfiles allows creating symlinks from a declarative<br>config, and you can use NixOS to<br>configure<br>systemd-tmpfiles itself (thanks, Noobz!).

For example, if I want to symlink ~/dotfiles/git/config<br>to .config/git/config:

systemd.tmpfiles.rules = [<br>"L+ /home/matklad/.config/git/config - - - - /home/matklad/dotfiles/git/config"<br>];

No opinion at this point how this compares to a bespoke script or<br>something more purpose-built.

config dotfiles nixos home symlinks symlinking

Related Articles