{ lib, pkgs, config, ... }: { options.secrets = { keys = lib.mkOption { type = lib.types.listOf lib.types.unspecified; default = []; description = "Keys to upload to server"; }; location = lib.mkOption { type = lib.types.path; default = "/var/secrets"; description = "Location where to put the keys"; }; # Read-only variables fullPaths = lib.mkOption { type = lib.types.attrsOf lib.types.path; default = builtins.listToAttrs (map (v: { name = v.dest; value = "${config.secrets.location}/${v.dest}"; }) config.secrets.keys); readOnly = true; description = "set of full paths to secrets"; }; }; config = let location = config.secrets.location; keys = config.secrets.keys; empty = pkgs.runCommand "empty" { preferLocalBuild = true; } "mkdir -p $out && touch $out/done"; dumpKey = v: '' mkdir -p secrets/$(dirname ${v.dest}) echo -n ${lib.strings.escapeShellArg v.text} > secrets/${v.dest} cat >> mods < 0) { system.activationScripts.secrets = { deps = [ "users" "wrappers" ]; text = '' install -m0750 -o root -g keys -d ${location} if [ -f /run/keys/secrets.tar ]; then if [ ! -f ${location}/currentSecrets ] || ! sha512sum -c --status "${location}/currentSecrets"; then echo "rebuilding secrets" TMP=$(${pkgs.coreutils}/bin/mktemp -d) if [ -n "$TMP" ]; then install -m0750 -o root -g keys -d $TMP ${pkgs.gnutar}/bin/tar --strip-components 1 -C $TMP -xf /run/keys/secrets.tar sha512sum /run/keys/secrets.tar > $TMP/currentSecrets find $TMP -type d -exec chown root:keys {} \; -exec chmod o-rx {} \; ${pkgs.rsync}/bin/rsync -O -c -av --delete $TMP/ ${location} rm -rf $TMP fi fi fi ''; }; system.extraDependencies = [ secrets ]; deployment.secrets."secrets.tar" = { source = "${secrets}"; destination = "/run/keys/secrets.tar"; owner.user = "root"; owner.group = "root"; permissions = "0400"; }; }; }