aboutsummaryrefslogtreecommitdiff
path: root/modules/secrets.nix
blob: ecc1ebc56a976023911fb1ac8b88968b6e9e85df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
{ 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";
    fpath = v: "secrets/${v.dest}${lib.optionalString (v.isTemplated or true) ".gucci.tpl"}";
    dumpKey = v: ''
        mkdir -p secrets/$(dirname ${v.dest})
        echo -n ${lib.strings.escapeShellArg v.text} > ${fpath v}
        cat >> mods <<EOF
        ${v.user or "root"} ${v.group or "root"} ${v.permissions or "0600"} ${fpath v}
        EOF
        '';
    secrets = pkgs.runCommand "secrets.tar" {} ''
      touch mods
      tar --format=ustar --mtime='1970-01-01' -P --transform="s@${empty}@secrets@" -cf $out ${empty}/done
      ${builtins.concatStringsSep "\n" (map dumpKey keys)}
      cat mods | while read u g p k; do
      tar --format=ustar --mtime='1970-01-01' --owner="$u" --group="$g" --mode="$p" --append -f $out "$k"
      done
      '';
  in lib.mkIf (builtins.length keys > 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
              if [ -f /run/keys/vars.yml ]; then
                find $TMP -name "*.gucci.tpl" -exec \
                  /bin/sh -c 'f="{}"; ${pkgs.gucci}/bin/gucci -f /run/keys/vars.yml "$f" > "''${f%.gucci.tpl}"; touch --reference "$f" ''${f%.gucci.tpl} ; chmod --reference="$f" ''${f%.gucci.tpl} ; chown --reference="$f" ''${f%.gucci.tpl}' \;
                sha512sum /run/keys/secrets.tar /run/keys/vars.yml > $TMP/currentSecrets
              else
                sha512sum /run/keys/secrets.tar > $TMP/currentSecrets
              fi
              find $TMP -type d -exec chown root:keys {} \; -exec chmod o-rx {} \;
              ${pkgs.rsync}/bin/rsync --exclude="*.gucci.tpl" -O -c -av --delete $TMP/ ${location}
              rm -rf $TMP
            fi
          fi
        fi
        '';
    };

    system.extraDependencies = [ secrets ];
    deployment.secrets."secret_vars.yml" = {
      source = builtins.toString <privateFiles/vars.yml>;
      destination = "/run/keys/vars.yml";
      owner.user = "root";
      owner.group = "root";
      permissions = "0400";
     };
    deployment.secrets."secrets.tar" = {
      source = "${secrets}";
      destination = "/run/keys/secrets.tar";
      owner.user = "root";
      owner.group = "root";
      permissions = "0400";
    };
  };
}