Github strategies for configuration.nix?


#1

Hi all-

I was wondering what GitHub strategies you all use to keep your configuration.nix file (and extra files that go with it) under version control.

The problem I’m having is that hardware-configuration.nix is generated by a new installation, along with a basic configuration.nix. If I then want to supply my own configuration.nix from GitHub or somewhere else, how would I go about it? Symlinks to a directory I check out into /etc?

roni


#2

/etc/nixos being a git repository is a common solution. I usually
clone it after I run nixos-generate-config
(https://nixos.org/nixos/manual/index.html#sec-installation-installing),
put the generated configs into hosts/${hostname} subdirectory,
customize it and then create a
symlink

before finishing the installation.

But you can place the configuration anywhere and run nixos-rebuild
with -I nixos-config=path/to/your/configuration.nix.


#3

I have a separate repository for my config and symlink the configuration from there into /etc/nixos. One reason this is nice is that I have configurations for several machines in the same repo, so they can share common modules and so on.


#4

This is amazing, thanks for providing your own configs as an example.

How do you actually wire this into your system? Do you have to symlink both configuration.nix and hardware-configuration.nix on a given machine to the files in the matching subdirectory of machines/? If you just symlink those, how do the relative import statements in, say, machines/clipper/configuration.nix work properly?

Thanks!

roni


#5

Thanks for the symlink script, and the tip about the -I flag!

roni


#6

You only have to symlink configuration.nix. As you’ve realised, the crucial fact is that the relative imports are resolved from the actual location of the file. So having symlinked in configuration.nix from machines/whatever/configuration.nix, the import ./hardware-configuration.nix resolves correctly to machines/whatever/hardware-configuration.nix.


#7

Good to know. I’ve run into problems caused by this exact behavior in Node projects, so I guess I’m once-bitten twice-shy. Thanks!

roni


#8

I manage all my systems via nixops with all configuration in a ~/nixops (including secrets, which are encrypted with git-crypt). To deploy a system I cd into it and run make $(hostname) which expands to nixops modify -d $(hostname) systems/$(hostname) && nixops deploy -d $(hostname) and some other commands (depending on the hostname). The target system requires an SSH server, even if you’re deploying locally.

Take note of nixops#736 when working with remote servers on a different release than your local system.


#9

I clone my repo with all my configuration after a nixos-generate-config like jtojnar and I imports the common part to the current configuration. After that I create a symlink from /etc/nixos/configuration.nix to my custom path /etc/nixos/systems/$(hostname).

I keep my git repo in /etc/nixos and I apply an acl on this folder to avoid sudo at each commit. (https://www.geeksforgeeks.org/access-control-listsacl-linux/)


#10

I don’t have an /etc/NixOS folder, but instead deploy directly from a relocatable git repo using a custom script: https://github.com/arianvp/nixos-stuff/blob/8e165977bd39b73c8dee5fc75d599d6bb35465f8/deploy-local.sh


#11

nix.nixPath = [ "nixos-config=/path/to/repo/machine.nix" ];


#12

I don’t keep my config in /etc/nixos/configuration.nix, it just includes other files and has the stateversion so it isn’t tracked in git. I use stow to manage my dotfiles and I keep my nix configuration in the same repo, though I may move it to it’s own repo. Stow is used to symlink the configuration. nixpkgs is currently a submodule in that repository and that is how I pin it, I’m not a big fan of that but I don’t know of anything better for me.

On a new machine I do the following (c/p from repo):

git clone git@github.com:borisbabic/dotfiles.git --recurse-submodules /etc/nixos/dotfiles # or using https git clone https://github.com/borisbabic/dotfiles.git --recurse-submodules /etc/nixos/dotfiles
cd /etc/nixos/dotfiles
stow nixos
cp nixos/configuration.nix.skel /etc/nixos/configuration.nix
vim /etc/nixos/configuration.nix #choose what you want, and update the state version to a newer one if avilable
sudo nixos-rebuild switch

#13

My dotfiles https://github.com/srghma/dotfiles/blob/6457ffdb5926a769c785f134c04ccdab83c0afac/README.md#how-to-install-on-new-computer

# COPY hardware-configuration.nix to dotfiles to be able to modify it without sudo
cp /mnt/etc/nixos/hardware-configuration.nix.old /mnt/home/srghma/.dotfiles/nixos/root/hardware-configuration.nix

# LINK CONFIGS TO CONFIGS IN DOTFILES

# this configuration is used only during installation
printf "import /mnt/home/srghma/.dotfiles/nixos/root/default.nix" > /mnt/etc/nixos/configuration.nix

#14

I do something similar to this:

  nix.nixPath = [
    "nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos"
    "nixos-config=/home/benley/p/dotfiles/machines/${config.networking.hostName}/configuration.nix"
    "/nix/var/nix/profiles/per-user/root/channels"
  ];

I keep my dotfiles repo cloned at ~/p/dotfiles, and when setting up a new system I run nixos-rebuild with an explicit -I /home/benley/...../configuration.nix once. From then on, as long as the system’s own hostname is set, it looks in the right place for its nixos-config.


#15

I deploy my nixos configuration via nixpkgs and an overlay:

My file /etc/nixos/configuration.nix just contains the following:

let mypkgs = import <nixpkgs> {}; in mypkgs.myconfig.nixos-config

and nixpkgs.myconfig.nixos-config added via the overlay https://github.com/maxhbr/myconfig/blob/a6f2f87cc546f518fe5f25b09121ddddb18d4425/default.nix which just imports the top level default.nix of my dotfiles: https://github.com/maxhbr/myconfig/blob/master/default.nix

To get that working I clone my dotfiles to ~/myconfig and the script ~/myconfig/rebuild.sh just sets up everything (e.g. adds nixpkgs-overlays=$HOME/myconfig/nix/overlays to my $NIX_PATH). I also use the hostname, defined in a file /etc/nixos/hostname to determine which configuration to deploy.

The complete (and tested) instructions how to bootstrap my nixos configuration in encapsulated in a packer script: https://github.com/maxhbr/myconfig/tree/master/misc/bootstrap


#16

I also did a talk about this as well as using Nixops and a nix-darwin haskell based deployment tool at NixCon this past year: https://www.youtube.com/watch?v=0pqdOnQKMKE