# agenix secrets. Every secret file is optional: a secret is only declared when # its .age file exists in `my.secrets.dir`, so a fresh clone still evaluates. # # -password.age hashed login password (mkpasswd -m yescrypt) # ssh-.age private SSH key, installed for the user (home-manager # links it to ~/.ssh/) { config, inputs, lib, pkgs, ... }: let cfg = config.my.secrets; user = config.my.user.name; secretFile = name: cfg.dir + "/${name}.age"; exists = name: builtins.pathExists (secretFile name); passwordSecrets = lib.optionalAttrs (exists "${user}-password") { "${user}-password" = { file = secretFile "${user}-password"; mode = "0400"; }; }; sshSecrets = lib.listToAttrs ( map ( key: lib.nameValuePair "ssh-${key}" { file = secretFile "ssh-${key}"; owner = user; mode = "0400"; } ) (lib.filter (key: exists "ssh-${key}") cfg.userSshKeys) ); in { options.my.secrets = { enable = lib.mkEnableOption "agenix secrets from the repository's secrets/ directory"; dir = lib.mkOption { type = lib.types.path; default = ../../../secrets; defaultText = lib.literalExpression "../../../secrets"; description = '' Directory holding the *.age files. In a flake only files tracked by git are visible, so `git add` new secrets before building. ''; }; userSshKeys = lib.mkOption { type = lib.types.listOf lib.types.str; default = [ "id_ed25519" ]; description = "SSH key names; each maps to secrets/ssh-.age if present."; }; }; config = lib.mkIf cfg.enable { # The host key is generated on the install USB (`create-keys`) and copied # to the target, so secrets can be decrypted on the very first boot. age.identityPaths = [ "/etc/ssh/ssh_host_ed25519_key" ]; age.secrets = passwordSecrets // sshSecrets; environment.systemPackages = [ inputs.agenix.packages.${pkgs.stdenv.hostPlatform.system}.default pkgs.age ]; }; }