I’ve been looking at ways to manage secrets in Nix, and have not yet chosen one.
Being new to Nix, I setup a Raspberry Pi as an exercise, with a few services, that each consume secrets from their config files.
As a first step, I chose the easiest approach I could think of: to separate my secrets from the configuration.nix file, by simply importing a separate “./secrets.nix” file from configuration.nix.
Once I got things working, I created a git repo, with configuration.nix, flake.nix, etc, and put the ./secrets.nix in .gitignore, and was surprised that nixos-rebuild was now searching for the secrets.nix in the nix store !
I then learned that nixos-rebuild requires all config to be in git (nixos-rebuild assumes that if the file is not in git, it’s in the nix store). I can understand why, but I’m not sure if I like this, and I tend to agree with this reddit poster Reddit - The heart of the internet
I don’t like the idea of being forced to put secrets in source control. I think there are valid reasons to do so (put encrypted secrets in git), but I don’t like the idea of forcing that choice.
I’m about to try out agenix, and I’m hopping it will allow me to keep secrets out of source control.
I’m mainly interested of secrets management for systemd services, and from what I notice, there is quite a bit of “diversity” in the way a nix service package decides to load it’s secrets, but from the services I have configured so far, they all generate a config file with their secrets inside.
The diversity so far is the location of config files :
-r--r--r-- 1 root root 751 Jan 1 1970 /nix/store/iflsvvfjgvyc81wrs21vpv33v5yp3kdl-zigbee2mqtt.yaml
-rw------- 1 mpd mpd 561 Nov 24 14:05 /var/run/mpd/mpd.conf
lrwxrwxrwx 1 root root 45 Nov 25 15:10 /etc/home-assistant/configuration.yaml -> /etc/static/home-assistant/configuration.yaml
One can observe “guidelines”, are not necessarily followed by whoever wrote the nix packages (secrets in /nix/store, permissions on /etc/home-assistant/configuration.yaml), and I’m sure each service I’ll install will bring new “diversity”.
I’m not a purist, so my only concern is that a package “consumer” (or user) has a way to compensate for whatever he finds unacceptable.
For example, I’m fine with /nix/store/iflsvvfjgvyc81wrs21vpv33v5yp3kdl-zigbee2mqtt.yaml ending up in /nix/store, because it’s owned by root. Perhaps there are reasons why I should still care, I’d like to know !
For /etc/home-assistant/configuration.yaml → /etc/static/home-assistant/configuration.yaml, I suppose there is an easy way to do a chmod from the nix config.
I looked at nixops, and thought that their solution had some elegance: each secret gets a systemd service that is responsible for decrypting the secret. Unfortunately the project is dead, not sure if nixops4 follows this idea.
It seems that secrets management falls in one of the two approaches :
-
secrets are decrypted at build time, stored in a config file (with hopefully correct permissions and location)
-
secrets are decrypted at serice lauch time and never saved to disk unencrypted (but a “master” secret may be stored unencrypted, for the machine to be bootable without intervention)
Ideally a secret management tool, should be able to support both.