Compare commits

...

5 Commits

Author SHA1 Message Date
John Lancaster fac9c7f5ce mtls param renames 2026-04-01 20:45:09 -05:00
John Lancaster a7b65e4eee formatting 2026-04-01 20:44:40 -05:00
John Lancaster 1fe4d59ce6 mtls options 2026-04-01 15:59:56 -05:00
John Lancaster 86bb800886 prune 2026-04-01 15:57:51 -05:00
John Lancaster c4cf7c8096 fixed postgres group 2026-04-01 15:57:44 -05:00
5 changed files with 90 additions and 88 deletions
+54 -51
View File
@@ -1,7 +1,7 @@
{ self, inputs, lib, ... }: { self, inputs, lib, ... }:
let let
# Options that will be in common between # Options that will be in common between the nixos module and the home-manager module.
opts = { mkOpts = config: let cfg = config.mtls; in {
enable = lib.mkEnableOption "Enable mTLS"; enable = lib.mkEnableOption "Enable mTLS";
subject = lib.mkOption { subject = lib.mkOption {
description = "The Common Name, DNS Name, or IP address that will be set as the Subject Common Name for the certificate. If no Subject Alternative Names (SANs) are configured (via the --san flag) then the subject will be set as the only SAN."; description = "The Common Name, DNS Name, or IP address that will be set as the Subject Common Name for the certificate. If no Subject Alternative Names (SANs) are configured (via the --san flag) then the subject will be set as the only SAN.";
@@ -11,20 +11,25 @@ let
description = "String path to the directory where the certs will be stored"; description = "String path to the directory where the certs will be stored";
type = lib.types.str; type = lib.types.str;
}; };
keyFilename = lib.mkOption { caFile = lib.mkOption {
description = "String filename for the private key"; description = "String path for the root CA file";
type = lib.types.str; type = lib.types.str;
default = "key.pem"; default = "${cfg.certDir}/root_ca.crt";
}; };
certFilename = lib.mkOption { keyFile = lib.mkOption {
description = "String filename for the public certificate"; description = "String path for the private key";
type = lib.types.str; type = lib.types.str;
default = "cert.pem"; default = "${cfg.certDir}/key.pem";
}; };
bundleFilename = lib.mkOption { certFile = lib.mkOption {
description = "String filename for the mTLS key bundle"; description = "String path for the public cert";
type = lib.types.str; type = lib.types.str;
default = "mtls.pem"; default = "${cfg.certDir}/cert.pem";
};
bundleFile = lib.mkOption {
description = "String path for the mTLS key bundle";
type = lib.types.str;
default = "${cfg.certDir}/mtls.pem";
}; };
san = lib.mkOption { san = lib.mkOption {
description = "List of SAN to give the mTLS cert"; description = "List of SAN to give the mTLS cert";
@@ -37,7 +42,7 @@ let
}; };
lifetime = lib.mkOption { lifetime = lib.mkOption {
type = lib.types.str; type = lib.types.str;
default = "6h"; default = "24h";
}; };
renew = { renew = {
enable = lib.mkOption { enable = lib.mkOption {
@@ -83,9 +88,9 @@ let
subject, subject,
provisioner, provisioner,
san, san,
tlsCert, certFile,
tlsKey, keyFile,
mtlsBundle, bundleFile,
lifetime, lifetime,
}: }:
let let
@@ -95,26 +100,26 @@ let
pkgs.writeShellScriptBin "mtls-generate" '' pkgs.writeShellScriptBin "mtls-generate" ''
set -euo pipefail set -euo pipefail
${stepCmd} ca certificate \ ${stepCmd} ca certificate \
${subject} ${tlsCert} ${tlsKey} \ ${subject} ${certFile} ${keyFile} \
--not-before=-5m --not-after=${lifetime} \ --not-before=-5m --not-after=${lifetime} \
--provisioner ${provisioner} \ --provisioner ${provisioner} \
${sanArgs} \ ${sanArgs} \
"$@" "$@"
cat ${tlsCert} ${tlsKey} > ${mtlsBundle} cat ${certFile} ${keyFile} > ${bundleFile}
''; '';
mkMtlsCheckScript = { pkgs, mtlsBundle }: pkgs.writeShellScriptBin "mtls-check" '' mkMtlsCheckScript = { pkgs, bundleFile }: pkgs.writeShellScriptBin "mtls-check" ''
${lib.getExe pkgs.openssl} x509 \ ${lib.getExe pkgs.openssl} x509 \
-noout -subject -issuer \ -noout -subject -issuer \
-ext subjectAltName,extendedKeyUsage \ -ext subjectAltName,extendedKeyUsage \
-enddate -in ${mtlsBundle} -enddate -in ${bundleFile}
''; '';
mkMtlsRenewScript = { mkMtlsRenewScript = {
pkgs, pkgs,
tlsCert, certFile,
tlsKey, keyFile,
mtlsBundle, bundleFile,
reloadUnits ? [ ], reloadUnits ? [ ],
postCommands ? [ ], postCommands ? [ ],
systemctlArgs ? [ ], systemctlArgs ? [ ],
@@ -134,17 +139,17 @@ let
pkgs.writeShellScriptBin "mtls-renew" '' pkgs.writeShellScriptBin "mtls-renew" ''
set -euo pipefail set -euo pipefail
if ${lib.getExe pkgs.step-cli} certificate needs-renewal "${tlsCert}"; then if ${lib.getExe pkgs.step-cli} certificate needs-renewal "${certFile}"; then
${echoCmd} "Renewing mTLS certificate" ${echoCmd} "Renewing mTLS certificate"
else else
${echoCmd} "Skipping renew" ${echoCmd} "Skipping renew"
exit "$?" exit "$?"
fi fi
${lib.getExe pkgs.step-cli} ca renew --force "${tlsCert}" "${tlsKey}" ${lib.getExe pkgs.step-cli} ca renew --force "${certFile}" "${keyFile}"
umask 077 umask 077
${lib.getExe' pkgs.coreutils "cat"} "${tlsCert}" "${tlsKey}" > "${mtlsBundle}" ${lib.getExe' pkgs.coreutils "cat"} "${certFile}" "${keyFile}" > "${bundleFile}"
${echoCmd} "Reloading units:" ${echoCmd} "Reloading units:"
${renewReloadScript} ${renewReloadScript}
@@ -155,9 +160,9 @@ let
mkNixosMtlsRenewService = { mkNixosMtlsRenewService = {
pkgs, pkgs,
tlsCert, certFile,
tlsKey, keyFile,
mtlsBundle, bundleFile,
reloadUnits ? [ ], reloadUnits ? [ ],
postCommands ? [ ], postCommands ? [ ],
user ? "root", user ? "root",
@@ -166,7 +171,7 @@ let
let let
serviceGroup = if group == null then user else group; serviceGroup = if group == null then user else group;
renewScript = mkMtlsRenewScript { renewScript = mkMtlsRenewScript {
inherit pkgs tlsCert tlsKey mtlsBundle reloadUnits postCommands; inherit pkgs certFile keyFile bundleFile reloadUnits postCommands;
}; };
in in
{ {
@@ -200,15 +205,15 @@ let
mkHomeManagerMtlsRenewService = { mkHomeManagerMtlsRenewService = {
pkgs, pkgs,
tlsCert, certFile,
tlsKey, keyFile,
mtlsBundle, bundleFile,
reloadUnits ? [ ], reloadUnits ? [ ],
postCommands ? [ ], postCommands ? [ ],
}: }:
let let
renewScript = mkMtlsRenewScript { renewScript = mkMtlsRenewScript {
inherit pkgs tlsCert tlsKey mtlsBundle reloadUnits postCommands; inherit pkgs certFile keyFile bundleFile reloadUnits postCommands;
systemctlArgs = [ "--user" ]; systemctlArgs = [ "--user" ];
}; };
in in
@@ -249,17 +254,14 @@ in
flake.modules.nixos.mtls = { config, lib, pkgs, ... }: flake.modules.nixos.mtls = { config, lib, pkgs, ... }:
let let
cfg = config.mtls; cfg = config.mtls;
tlsKey = "${cfg.certDir}/${cfg.keyFilename}";
tlsCert = "${cfg.certDir}/${cfg.certFilename}";
mtlsBundle = "${cfg.certDir}/${cfg.bundleFilename}";
sanArgs = lib.concatMapStringsSep " " (san: "--san \"${san}\"") cfg.san; sanArgs = lib.concatMapStringsSep " " (san: "--san \"${san}\"") cfg.san;
in in
{ {
options.mtls = opts // { options.mtls = (mkOpts config) // {
certDir = lib.mkOption { certDir = lib.mkOption {
description = "String path to where the mtls certs will be stored."; description = "String path to where the mtls certs will be stored.";
type = lib.types.str; type = lib.types.str;
default = "/etc/step/certs"; default = "/etc/step-ca/certs";
}; };
}; };
@@ -267,11 +269,11 @@ in
environment.systemPackages = with pkgs; lib.optionals cfg.enable [ environment.systemPackages = with pkgs; lib.optionals cfg.enable [
# step-cli # step-cli
(mkMtlsGenerateScript { (mkMtlsGenerateScript {
inherit (cfg) subject provisioner san lifetime; inherit pkgs;
inherit pkgs tlsCert tlsKey mtlsBundle; inherit (cfg) subject provisioner san certFile keyFile bundleFile lifetime;
}) })
(mkMtlsCheckScript { inherit pkgs mtlsBundle; }) (mkMtlsCheckScript { inherit pkgs; inherit (cfg) bundleFile; })
(mkMtlsRenewScript { inherit pkgs tlsCert tlsKey mtlsBundle; }) (mkMtlsRenewScript { inherit pkgs; inherit (cfg) certFile keyFile bundleFile; })
]; ];
systemd.tmpfiles.rules = [ systemd.tmpfiles.rules = [
@@ -279,7 +281,8 @@ in
]; ];
systemd.services.mtls-renew = lib.mkIf cfg.renew.enable (mkNixosMtlsRenewService { systemd.services.mtls-renew = lib.mkIf cfg.renew.enable (mkNixosMtlsRenewService {
inherit pkgs tlsCert tlsKey mtlsBundle; inherit pkgs;
inherit (cfg) certFile keyFile bundleFile;
inherit (cfg.renew) reloadUnits postCommands user group; inherit (cfg.renew) reloadUnits postCommands user group;
}); });
@@ -292,13 +295,13 @@ in
flake.modules.homeManager.mtls = { config, lib, pkgs, ... }: flake.modules.homeManager.mtls = { config, lib, pkgs, ... }:
let let
cfg = config.mtls; cfg = config.mtls;
tlsKey = "${cfg.certDir}/${cfg.keyFilename}"; keyFile = cfg.keyFile;
tlsCert = "${cfg.certDir}/${cfg.certFilename}"; certFile = cfg.certFile;
mtlsBundle = "${cfg.certDir}/${cfg.bundleFilename}"; bundleFile = cfg.bundleFile;
sanArgs = lib.concatMapStringsSep " " (san: "--san \"${san}\"") cfg.san; sanArgs = lib.concatMapStringsSep " " (san: "--san \"${san}\"") cfg.san;
in in
{ {
options.mtls = opts // { options.mtls = (mkOpts config) // {
certDir = lib.mkOption { certDir = lib.mkOption {
description = "String path to where the mtls certs will be stored."; description = "String path to where the mtls certs will be stored.";
type = lib.types.str; type = lib.types.str;
@@ -311,10 +314,10 @@ in
# step-cli # step-cli
(mkMtlsGenerateScript { (mkMtlsGenerateScript {
inherit (cfg) subject provisioner san lifetime; inherit (cfg) subject provisioner san lifetime;
inherit pkgs tlsCert tlsKey mtlsBundle; inherit pkgs certFile keyFile bundleFile;
}) })
(mkMtlsCheckScript { inherit pkgs mtlsBundle; }) (mkMtlsCheckScript { inherit pkgs bundleFile; })
(mkMtlsRenewScript { inherit pkgs tlsCert tlsKey mtlsBundle; }) (mkMtlsRenewScript { inherit pkgs certFile keyFile bundleFile; })
]; ];
systemd.user.tmpfiles.rules = lib.mkIf cfg.enable [ systemd.user.tmpfiles.rules = lib.mkIf cfg.enable [
@@ -322,7 +325,7 @@ in
]; ];
systemd.user.services.mtls-renew = lib.mkIf cfg.renew.enable (mkHomeManagerMtlsRenewService { systemd.user.services.mtls-renew = lib.mkIf cfg.renew.enable (mkHomeManagerMtlsRenewService {
inherit pkgs tlsCert tlsKey mtlsBundle; inherit pkgs certFile keyFile bundleFile;
inherit (cfg.renew) reloadUnits postCommands; inherit (cfg.renew) reloadUnits postCommands;
}); });
+12 -12
View File
@@ -19,6 +19,11 @@
type = lib.types.str; type = lib.types.str;
default = "john-ubuntu"; default = "john-ubuntu";
}; };
repoUrl = lib.mkOption {
description = "URL to the REST endpoint";
type = lib.types.str;
default = "rest:https://soteria.john-stream.com/${cfg.repoName}";
};
passwordFile = lib.mkOption { passwordFile = lib.mkOption {
description = "String path to the restic password file"; description = "String path to the restic password file";
type = lib.types.str; type = lib.types.str;
@@ -44,29 +49,24 @@
}; };
}; };
config = let config = {
resticRepository = "rest:https://soteria.john-stream.com/${cfg.repoName}";
caCert = "${config.mtls.certDir}/root_ca.crt";
mtlsBundle = "${config.mtls.certDir}/${config.mtls.bundleFilename}";
in
{
home.sessionVariables = { home.sessionVariables = {
RESTIC_REPOSITORY = resticRepository; RESTIC_REPOSITORY = cfg.repoUrl;
RESTIC_PASSWORD_FILE = cfg.passwordFile; RESTIC_PASSWORD_FILE = cfg.passwordFile;
RESTIC_CACERT = caCert; RESTIC_CACERT = config.mtls.caFile;
RESTIC_TLS_CLIENT_CERT = mtlsBundle; RESTIC_TLS_CLIENT_CERT = config.mtls.bundleFile;
}; };
# This is necessary because the restic service in home manager doesn't otherwise expose these options. # This is necessary because the restic service in home manager doesn't otherwise expose these options.
systemd.user.services."restic-backups-${cfg.repoName}".Service.Environment = [ systemd.user.services."restic-backups-${cfg.repoName}".Service.Environment = [
"RESTIC_CACERT=${caCert}" "RESTIC_CACERT=${config.mtls.caFile}"
"RESTIC_TLS_CLIENT_CERT=${mtlsBundle}" "RESTIC_TLS_CLIENT_CERT=${config.mtls.bundleFile}"
]; ];
services.restic = { services.restic = {
enable = true; enable = true;
backups.${cfg.repoName} = { backups.${cfg.repoName} = {
repository = resticRepository; repository = cfg.repoUrl;
passwordFile = cfg.passwordFile; passwordFile = cfg.passwordFile;
paths = cfg.paths; paths = cfg.paths;
timerConfig = { timerConfig = {
+22 -21
View File
@@ -6,27 +6,29 @@ let
fingerprint = "2036c44f7b5901566ff7611ea6c927291ecc6d2dd00779c0eead70ec77fa10d6"; fingerprint = "2036c44f7b5901566ff7611ea6c927291ecc6d2dd00779c0eead70ec77fa10d6";
in in
{ {
flake.modules.nixos.janus-ca = { config, lib, ... }: flake.modules.nixos.janus-ca =
let { config, lib, ... }:
johnHome = lib.attrByPath [ "users" "users" username "home" ] "/home/${username}" config; let
johnGroup = lib.attrByPath [ "users" "users" username "group" ] username config; johnHome = lib.attrByPath [ "users" "users" username "home" ] "/home/${username}" config;
mkStepRules = home: user: group: [ johnGroup = lib.attrByPath [ "users" "users" username "group" ] username config;
"d ${home}/.step 0700 ${user} ${group} -" mkStepRules = home: user: group: [
"d ${home}/.step/config 0700 ${user} ${group} -" "d ${home}/.step 0700 ${user} ${group} -"
"d ${home}/.step/certs 0700 ${user} ${group} -" "d ${home}/.step/config 0700 ${user} ${group} -"
"L+ ${home}/.step/config/defaults.json - - - - /etc/step/config/defaults.json" "d ${home}/.step/certs 0700 ${user} ${group} -"
"L+ ${home}/.step/certs/root_ca.crt - - - - /etc/step/certs/root_ca.crt" "L+ ${home}/.step/config/defaults.json - - - - /etc/step/config/defaults.json"
]; "L+ ${home}/.step/certs/root_ca.crt - - - - /etc/step/certs/root_ca.crt"
in { ];
environment.etc."step/config/defaults.json".text = builtins.toJSON { in
inherit ca-url fingerprint; {
root = "/etc/step/certs/root_ca.crt"; environment.etc."step/config/defaults.json".text = builtins.toJSON {
inherit ca-url fingerprint;
root = "/etc/step-ca/certs/root_ca.crt";
};
environment.etc."step-ca/certs/root_ca.crt".source = ./root_ca.crt;
systemd.tmpfiles.rules =
mkStepRules johnHome username johnGroup
++ mkStepRules "/root" "root" "root";
}; };
environment.etc."step/certs/root_ca.crt".source = ./root_ca.crt;
systemd.tmpfiles.rules =
mkStepRules johnHome username johnGroup
++ mkStepRules "/root" "root" "root";
};
flake.modules.homeManager.janus-ca = { config, ... }: { flake.modules.homeManager.janus-ca = { config, ... }: {
home.file.".step/config/defaults.json".text = builtins.toJSON { home.file.".step/config/defaults.json".text = builtins.toJSON {
@@ -73,7 +75,6 @@ in
]; ];
}; };
flake-file.inputs = { flake-file.inputs = {
wrappers = { wrappers = {
url = "github:lassulus/wrappers"; url = "github:lassulus/wrappers";
-2
View File
@@ -12,8 +12,6 @@ in
flake.modules.homeManager."${hostname}" = { config, pkgs, lib, ... }: flake.modules.homeManager."${hostname}" = { config, pkgs, lib, ... }:
let let
flakeDir = "${config.xdg.configHome}/home-manager/jsl-dendritic"; flakeDir = "${config.xdg.configHome}/home-manager/jsl-dendritic";
certDir = "${config.mtls.certDir}";
mtlsBundle = "${certDir}/${config.mtls.bundleFilename}";
resticPasswordFile = "${config.xdg.configHome}/restic/password.txt"; resticPasswordFile = "${config.xdg.configHome}/restic/password.txt";
testPushCmd = (pkgs.writeShellScriptBin "test-push" '' testPushCmd = (pkgs.writeShellScriptBin "test-push" ''
+2 -2
View File
@@ -22,9 +22,9 @@
++ lib.optional isAdmin "wheel" ++ lib.optional isAdmin "wheel"
++ lib.optional config.virtualisation.docker.enable "docker" ++ lib.optional config.virtualisation.docker.enable "docker"
++ lib.optional (isAdmin && config.services.forgejo.enable) config.services.forgejo.group ++ lib.optional (isAdmin && config.services.forgejo.enable) config.services.forgejo.group
++ lib.optional (isAdmin && config.services.postgresql.enable) config.services.postgresql.group; ++ lib.optional (isAdmin && config.services.postgresql.enable) "postgres";
}; };
security.sudo-rs.enable = lib.mkIf isAdmin true; security.sudo-rs.enable = lib.mkIf isAdmin true;
home-manager.useGlobalPkgs = true; home-manager.useGlobalPkgs = true;