Personal Nix setup

Add genCerts script

Changed files
+79
lib
+5
flake.nix
···
inherit (inputs.agenix.packages.${system}) agenix;
inherit (inputs.darwin.packages.${system}) darwin-rebuild;
});
+
+
apps = eachSystem (system: import ./lib/apps {
+
inherit lib;
+
pkgs = inputs.nixpkgs.legacyPackages.${system};
+
});
};
}
+6
lib/apps/default.nix
···
+
inputs: {
+
genCerts = {
+
type = "app";
+
program = import ./genCerts.nix inputs;
+
};
+
}
+68
lib/apps/genCerts.nix
···
+
{ lib, pkgs, ... }:
+
+
with lib;
+
let
+
cfssl = "${pkgs.cfssl}/bin/cfssl";
+
cfssljson = "${pkgs.cfssl}/bin/cfssljson";
+
+
caConf = pkgs.writeText "ca-conf.json" (builtins.toJSON {
+
signing = {
+
profiles = listToAttrs (map ({ name, extra ? [ ] }:
+
nameValuePair name {
+
usages = extra ++ [ "signing" "key encipherment" "server auth" "client auth" ];
+
expiry = "87600h";
+
}
+
) [
+
{ name = "auth-only"; }
+
{ name = "auth-and-cert-sign"; extra = [ "cert sign" ]; }
+
]);
+
};
+
});
+
+
mkCSR = name: pkgs.writeText "csr.json" (builtins.toJSON {
+
CN = name;
+
key = { algo = "rsa"; size = 4096; };
+
hosts = [ ];
+
});
+
+
mkGenCertCommand = { name, output, settings }: let
+
csr = mkCSR name;
+
args = attrsets.mapAttrsToList
+
(attr: value: if value == true then "-${attr}" else "-${attr}=${toString value}")
+
settings;
+
in ''
+
if [[ ! -f "${output}${name}.crt" ]]; then
+
mkdir -p "${output}"
+
${cfssl} gencert ${concatStringsSep " " args} \
+
${csr} | ${cfssljson} -bare "${output}/${name}"
+
rm "${output}/${name}.csr"
+
mv "${output}/${name}-key.pem" "${output}/${name}.key"
+
mv "${output}/${name}.pem" "${output}/${name}.crt"
+
fi
+
'';
+
+
caCertificate = {
+
name = "ca";
+
output = "modules/base/encrypt/";
+
settings.initca = true;
+
};
+
+
certificates = [
+
{
+
name = "mqtt";
+
output = "modules/automation/encrypt/";
+
settings = {
+
profile = "auth-only";
+
config = caConf;
+
ca = with caCertificate; "${output}/${name}.crt";
+
ca-key = with caCertificate; "${output}/${name}.key";
+
};
+
}
+
];
+
in
+
toString (pkgs.writers.writeBash "genCerts" ''
+
set -e
+
cd "$DIR"
+
${mkGenCertCommand caCertificate}
+
${concatStringsSep "\n" (map mkGenCertCommand certificates)}
+
'')