Moving to nix for dotfiles

Hey! I’m trying to move my dotfiles setup from a bunch of scripts to nix. I’m on macos, and bounced off it once before (mostly because of the learning curve.)

The thing I’m trying to figure out, basically, is how to get tools which expect things to be in well-known locations on disk to either modify their expectations or get nix to put them in the right places.

As an example of the first, I want to make an SSH configuration that sources from several sources to get all the hosts I care about. I think I can do this by writing a configuration file somewhere that has the relevant Include directives and wrapping ssh in an alias that does ssh -F /some/nix/store/config, but how do I get it to include all the right things?

As an example of the second, git expects that it will be able to read a global configuration from ~/.gitconfig (among other places.) You can set GIT_CONFIG, but doing so makes git ignore the local repo config (and so ignores remote configuration.) I’ve always used both to be able to set a username and email globally, and keep per-repo configuration in the relevant repo (for example, those remotes, or setting my work email address in work repos.) So it seems like I need to be able to write ~/.gitconfig or $XDG_HOME/git/config as part of derivation. How would I do this?

3 Likes

You may want to look at home-manager (or one of its alternatives). It is a tool for managing a user environment and includes modules for various programs, including SSH and git:

https://rycee.github.io/home-manager/options.html

Your configuration is stored in the nix store, but dotfiles are symlinked into your home directory. I use home-manager on the Mac, this is the configuration that I use:

https://github.com/danieldk/nix-home/blob/master/machines/macbook.nix

which is heavily inspired by John Wiegley’s configuration:

5 Likes

Add ~/.nix-profile/etc to $XDG_CONFIG_DIRS, and install an expression like writeTextFile {name = "config"; destination = "/etc/"; contents = ''…'';} into your profile.

That is, if you don’t want to use home-manager as mentioned by @danieldk which is more powerful and versatile, but at the cost of (arguably) being more complex.

3 Likes

whoah, thank you both.

I’m going to try home-manager, but now I know about writeTextFile so bonus! :smiley:

ok, wow, that worked splendidly. This has been a very pleasant experience compared to the first time I tried nix! Thank you both, again!

Here’s where I ended up: GitHub - BrianHicks/dotfiles.nix

4 Likes

Just came across a dotfiles repo that builds on this idea (although it uses writeScriptBin):
https://github.com/luke-clifton/nix-config

(Function docs are in the Nixpkgs manual, 8.6. Trivial builders and in the source in trivial-builders.nix.)

I see people by default recommending home-manager and while it’s a novel piece of work- it has a lot of downsides compared to stow. stow creates links which means I can edit ~/.bashrc and on the weekend go to ~/mygitrepo/dotfiles/.bashrc and do a git commit. With home-manager you lose this. I have the configs of a lot of non-standard programs stored this way (e.g. QtCreator.ini, alacritty et al).

I would personally recommend just using gnu stow or roll out your own symlinking.

4 Likes