sops with wrappers

This commit is contained in:
John Lancaster
2026-04-20 21:13:31 -05:00
parent e75951318d
commit 026bf541e1
+57 -56
View File
@@ -1,4 +1,4 @@
{ inputs, ... }: { self, inputs, ... }:
let let
inputs' = inputs; # save a reference before it's shadowed inputs' = inputs; # save a reference before it's shadowed
in in
@@ -16,16 +16,9 @@ in
# Define the homeModules that are used by flake-parts # Define the homeModules that are used by flake-parts
# https://flake.parts/options/home-manager.html#opt-flake.modules.homeManager # https://flake.parts/options/home-manager.html#opt-flake.modules.homeManager
flake.modules.homeManager.mysops = { inputs, config, pkgs, lib, ... }: flake.modules.homeManager.mysops = { config, pkgs, lib, ... }:
let let
cfg = config.mysops; cfg = config.mysops;
sopsBin = lib.getExe pkgs.sops;
sopsConfigPath = ../../.sops.yaml;
sopsSecretsPath = ../../keys/secrets.yaml;
editScript = lib.optional (cfg.hostSecretFile != null) (pkgs.writeShellScriptBin "edit-secrets" ''
${sopsBin} --config ${sopsConfigPath} ${cfg.hostSecretFile}
'');
in in
{ {
imports = [ imports = [
@@ -34,11 +27,6 @@ in
]; ];
options.mysops = { options.mysops = {
ageKeyFile = lib.mkOption {
description = "Default location for the age key";
type = lib.types.str;
default = "${config.xdg.configHome}/sops/age/keys.txt";
};
hostSecretFile = lib.mkOption { hostSecretFile = lib.mkOption {
description = "Path to the secrets file for this host. Used to create the edit-secrets script"; description = "Path to the secrets file for this host. Used to create the edit-secrets script";
type = lib.types.nullOr lib.types.str; type = lib.types.nullOr lib.types.str;
@@ -47,50 +35,63 @@ in
}; };
config = config =
let let
echo = lib.getExe' pkgs.coreutils "echo"; my-sops = (inputs.self.wrappers.mySops.apply {
dirname = lib.getExe' pkgs.coreutils "dirname"; inherit pkgs;
mkdir = lib.getExe' pkgs.coreutils "mkdir"; sshKey = config.ssh.identityFile;
show-age-key = (pkgs.writeShellScriptBin "show-age-key" '' }).wrapper;
${lib.getExe' pkgs.age "age-keygen"} -y ${cfg.ageKeyFile} in
''); {
in home.packages = with pkgs; [
{ my-sops
home.packages = with pkgs; [ (inputs.wrappers.lib.wrapPackage {
age binName = "ls-secrets";
sops # This is necessary to make the sops binary available inherit pkgs;
ssh-to-age package = inputs.self.packages.${pkgs.stdenv.hostPlatform.system}.my-eza;
(writeShellScriptBin "gen-age-key" '' args = [
set -eu "-T" "--follow-symlinks"
"${config.xdg.configHome}/sops-nix/secrets"
];
})
(inputs.wrappers.lib.wrapPackage {
binName = "edit-secrets";
inherit pkgs;
package = my-sops;
args = [ cfg.hostSecretFile ];
})
];
if [ ! -f "${config.ssh.identityFile}" ]; then # Option definitions for the sops home-manager module:
${echo} "SSH identity file not found: ${config.ssh.identityFile}" >&2 # https://github.com/Mic92/sops-nix/blob/master/modules/home-manager/sops.nix
exit 1 sops = {
fi defaultSopsFile = ../../keys/secrets.yaml;
defaultSopsFormat = "yaml";
age.sshKeyPaths = [ "${config.ssh.identityFile}" ];
};
};
};
if [ -e "${cfg.ageKeyFile}" ]; then flake.wrappers.mySops = inputs.wrappers.lib.wrapModule ({config, lib, wlib, ... }: {
${echo} "Refusing to overwrite existing age key file: ${cfg.ageKeyFile}" >&2 options = {
exit 1 sshKey = lib.mkOption {
fi type = lib.types.str;
description = "String path to the SSH key to use for creating an AGE key at runtime";
${mkdir} -p "$(${dirname} "${cfg.ageKeyFile}")"
${lib.getExe pkgs.ssh-to-age} -i ${config.ssh.identityFile} -private-key > ${cfg.ageKeyFile}
${echo} -n "Created ${cfg.ageKeyFile}: "
${echo} $(${lib.getExe show-age-key})
'')
show-age-key
(writeShellScriptBin "ls-secrets" "${lib.getExe pkgs.eza} -alT --follow-symlinks ~/.config/sops-nix/secrets")
] ++ editScript;
home.shellAliases.sops = "${sopsBin} --config ${sopsConfigPath}";
# Option definitions for the sops home-manager module:
# https://github.com/Mic92/sops-nix/blob/master/modules/home-manager/sops.nix
sops = {
defaultSopsFile = sopsSecretsPath;
defaultSopsFormat = "yaml";
age.sshKeyPaths = [ "${config.ssh.identityFile}" ];
}; };
}; };
};
config = {
# binName = "my-sops";
package = config.pkgs.sops;
extraPackages = with config.pkgs; [ coreutils ssh-to-age ];
preHook = ''
AGE_KEY=$(umask 077; mktemp)
ssh-to-age -private-key -i ${config.sshKey} > "$AGE_KEY"
'';
flags."--config" = "${../../.sops.yaml}";
postHook = ''
rm "$AGE_KEY"
echo "Removed $AGE_KEY"
'';
};
});
} }