Compare commits

...

8 Commits

19 changed files with 151 additions and 134 deletions

1
.envrc Normal file
View File

@@ -0,0 +1 @@
use flake

3
.gitignore vendored
View File

@@ -9,4 +9,5 @@ result-*
nixos-efi-vars.fd nixos-efi-vars.fd
/.pre-commit-config.yaml .direnv/
.pre-commit-config.yaml

View File

@@ -21,11 +21,7 @@ in
inputs.colmena.lib.makeHive ( inputs.colmena.lib.makeHive (
{ {
meta = { meta = {
nixpkgs = import inputs.nixpkgs { nixpkgs = import inputs.nixpkgs { localSystem = "x86_64-linux"; };
localSystem = "x86_64-linux";
};
nodeNixpkgs = builtins.mapAttrs (_: v: v.pkgs) self.nixosConfigurations;
specialArgs = { specialArgs = {
inherit inputs; inherit inputs;
outputs = self; outputs = self;

12
flake.lock generated
View File

@@ -284,11 +284,11 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1775585728, "lastModified": 1776796298,
"narHash": "sha256-8Psjt+TWvE4thRKktJsXfR6PA/fWWsZ04DVaY6PUhr4=", "narHash": "sha256-PcRvlWayisPSjd0UcRQbhG8Oqw78AcPE6x872cPRHN8=",
"owner": "cachix", "owner": "cachix",
"repo": "git-hooks.nix", "repo": "git-hooks.nix",
"rev": "580633fa3fe5fc0379905986543fd7495481913d", "rev": "3cfd774b0a530725a077e17354fbdb87ea1c4aad",
"type": "github" "type": "github"
}, },
"original": { "original": {
@@ -398,10 +398,10 @@
"nix-secrets": { "nix-secrets": {
"flake": false, "flake": false,
"locked": { "locked": {
"lastModified": 1776422417, "lastModified": 1776723456,
"narHash": "sha256-9R4MePj/UT0tqkWEq4Afg7Lp/zdfYHkW+qmpVGchKIs=", "narHash": "sha256-GBbbm05oXYqSZ2EgxQPsNpTKl16wNhvrlUxdmv0FbSU=",
"ref": "main", "ref": "main",
"rev": "75759a14e8d46421fca4306393a38b5ad5240f09", "rev": "135b681d24af6ee4508bbf7c657982d7be8743d4",
"shallow": true, "shallow": true,
"type": "git", "type": "git",
"url": "ssh://git@github.com/hektor/nix-secrets" "url": "ssh://git@github.com/hektor/nix-secrets"

View File

@@ -25,6 +25,7 @@
../../modules/nvim ../../modules/nvim
../../modules/pandoc ../../modules/pandoc
../../modules/photography ../../modules/photography
../../modules/secrets
../../modules/shell ../../modules/shell
../../modules/ssh ../../modules/ssh
../../modules/taskwarrior ../../modules/taskwarrior

View File

@@ -13,10 +13,12 @@ let
standalone = osConfig == null; standalone = osConfig == null;
in in
lib.optionalAttrs standalone { lib.optionalAttrs standalone {
sops.secrets = myUtils.mkSopsSecrets "${toString inputs.nix-secrets}/secrets" "anki" [ sops.secrets = myUtils.mkSopsSecrets "${toString inputs.nix-secrets}/secrets" null {
anki = [
"sync-user" "sync-user"
"sync-key" "sync-key"
] { }; ];
};
} }
// { // {
warnings = lib.optional ( warnings = lib.optional (

View File

@@ -15,11 +15,13 @@ let
in in
lib.optionalAttrs standalone { lib.optionalAttrs standalone {
sops = { sops = {
secrets = myUtils.mkSopsSecrets "${toString inputs.nix-secrets}/secrets" "taskwarrior" [ secrets = myUtils.mkSopsSecrets "${toString inputs.nix-secrets}/secrets" null {
taskwarrior = [
"sync-server-url" "sync-server-url"
"sync-server-client-id" "sync-server-client-id"
"sync-encryption-secret" "sync-encryption-secret"
] { }; ];
};
templates."taskrc.d/sync" = { templates."taskrc.d/sync" = {
content = '' content = ''

View File

@@ -57,6 +57,7 @@ in
secrets.nixSigningKey.enable = true; secrets.nixSigningKey.enable = true;
restic-backup.enable = true;
tailscale.enable = true; tailscale.enable = true;
docker.enable = true; docker.enable = true;

View File

@@ -53,6 +53,7 @@ in
secrets.nixSigningKey.enable = true; secrets.nixSigningKey.enable = true;
restic-backup.enable = true;
tailscale.enable = true; tailscale.enable = true;
docker.enable = true; docker.enable = true;
nfc.enable = true; nfc.enable = true;

View File

@@ -9,9 +9,12 @@
./host.nix ./host.nix
../../modules/common ../../modules/common
../../modules/ssh ../../modules/ssh
../../modules/tailscale
# ../../modules/uptime-kuma # ../../modules/uptime-kuma
]; ];
tailscale.enable = true;
ssh = { ssh = {
authorizedHosts = [ authorizedHosts = [
"andromache" "andromache"

View File

@@ -1,15 +1,14 @@
{ config, myUtils, ... }: { config, ... }:
let let
inherit (config.secrets) sopsDir;
inherit (config.host) username; inherit (config.host) username;
owner = config.users.users.${username}.name; inherit (config.secrets) owner;
in in
{ {
config.sops = { config = {
secrets = myUtils.mkSopsSecrets sopsDir "opencode" [ "api-key" ] { inherit owner; }; secrets.groups.opencode = [ "api-key" ];
templates."opencode/auth.json" = { sops.templates."opencode/auth.json" = {
inherit owner; inherit owner;
path = "/home/${username}/.local/share/opencode/auth.json"; path = "/home/${username}/.local/share/opencode/auth.json";
content = '' content = ''

View File

@@ -1,12 +1,6 @@
{ config, myUtils, ... }:
let
inherit (config.secrets) sopsDir;
inherit (config.host) username;
owner = config.users.users.${username}.name;
in
{ {
config.sops = { config.secrets.groups.anki = [
secrets = myUtils.mkSopsSecrets sopsDir "anki" [ "sync-user" "sync-key" ] { inherit owner; }; "sync-user"
}; "sync-key"
];
} }

View File

@@ -1,21 +1,16 @@
{ {
lib, lib,
config, config,
myUtils,
... ...
}: }:
let let
cfg = config.restic-backup; cfg = config.restic-backup;
inherit (config.secrets) sopsDir; host = config.networking.hostName;
in in
{ {
options = { options.restic-backup = {
restic-backup = { enable = lib.mkEnableOption "restic backups";
repository = lib.mkOption {
type = lib.types.str;
default = "b2:${config.sops.placeholder."backblaze-b2/bucket-name"}:${config.networking.hostName}";
};
passwordFile = lib.mkOption { passwordFile = lib.mkOption {
type = lib.types.str; type = lib.types.str;
@@ -27,31 +22,32 @@ in
default = [ "/home" ]; default = [ "/home" ];
}; };
}; };
config = lib.mkIf cfg.enable {
secrets.groups = {
restic = [ "password" ];
backblaze-b2 = [
"bucket-name"
"account-id"
"account-key"
];
}; };
config = { sops.templates = {
sops = { "restic/repo-${host}" = {
secrets = lib.mkMerge [ content = "b2:${config.sops.placeholder."backblaze-b2/bucket-name"}:${host}";
(myUtils.mkSopsSecrets sopsDir "restic" [ "password" ] { })
(myUtils.mkSopsSecrets sopsDir "backblaze-b2" [ "bucket-name" "account-id" "account-key" ] { })
];
templates = {
"restic/repo-${config.networking.hostName}" = {
content = "b2:${config.sops.placeholder."backblaze-b2/bucket-name"}:${config.networking.hostName}";
}; };
"restic/b2-env-${config.networking.hostName}" = { "restic/b2-env-${host}" = {
content = '' content = ''
B2_ACCOUNT_ID=${config.sops.placeholder."backblaze-b2/account-id"} B2_ACCOUNT_ID=${config.sops.placeholder."backblaze-b2/account-id"}
B2_ACCOUNT_KEY=${config.sops.placeholder."backblaze-b2/account-key"} B2_ACCOUNT_KEY=${config.sops.placeholder."backblaze-b2/account-key"}
''; '';
}; };
}; };
};
services.restic.backups.home = { services.restic.backups.home = {
repositoryFile = config.sops.templates."restic/repo-${config.networking.hostName}".path; repositoryFile = config.sops.templates."restic/repo-${host}".path;
inherit (cfg) passwordFile; inherit (cfg) passwordFile paths;
inherit (cfg) paths;
timerConfig = { timerConfig = {
OnCalendar = "daily"; OnCalendar = "daily";
Persistent = true; Persistent = true;
@@ -64,7 +60,7 @@ in
"--keep-monthly 6" "--keep-monthly 6"
"--keep-yearly 1" "--keep-yearly 1"
]; ];
environmentFile = config.sops.templates."restic/b2-env-${config.networking.hostName}".path; environmentFile = config.sops.templates."restic/b2-env-${host}".path;
}; };
}; };
} }

View File

@@ -1,14 +1,13 @@
{ {
lib, lib,
config, config,
myUtils,
... ...
}: }:
let let
cfg = config.hcloud; cfg = config.hcloud;
inherit (config.host) username; inherit (config.host) username;
inherit (config.secrets) sopsDir; inherit (config.secrets) owner;
in in
{ {
options.hcloud = { options.hcloud = {
@@ -16,12 +15,10 @@ in
}; };
config = lib.mkIf cfg.enable { config = lib.mkIf cfg.enable {
sops.secrets = myUtils.mkSopsSecrets sopsDir "hcloud" [ "api-token" ] { secrets.groups.hcloud = [ "api-token" ];
owner = config.users.users.${username}.name;
};
sops.templates."hcloud/cli.toml" = { sops.templates."hcloud/cli.toml" = {
owner = config.users.users.${username}.name; inherit owner;
path = "/home/${username}/.config/hcloud/cli.toml"; path = "/home/${username}/.config/hcloud/cli.toml";
content = '' content = ''
active_context = "server" active_context = "server"

View File

@@ -12,7 +12,6 @@ let
inherit (config.host) username; inherit (config.host) username;
inherit (cfg) sopsDir; inherit (cfg) sopsDir;
owner = config.users.users.${username}.name; owner = config.users.users.${username}.name;
mkSopsSecrets = myUtils.mkSopsSecrets sopsDir;
in in
{ {
imports = [ inputs.sops-nix.nixosModules.sops ]; imports = [ inputs.sops-nix.nixosModules.sops ];
@@ -24,6 +23,15 @@ in
default = "${toString inputs.nix-secrets}/secrets"; default = "${toString inputs.nix-secrets}/secrets";
}; };
groups = lib.mkOption {
type = lib.types.attrsOf (lib.types.listOf lib.types.str);
default = { };
};
owner = lib.mkOption {
type = lib.types.unspecified;
};
nixSigningKey = { nixSigningKey = {
enable = lib.mkEnableOption "nix signing key configuration"; enable = lib.mkEnableOption "nix signing key configuration";
}; };
@@ -35,27 +43,28 @@ in
}; };
config = { config = {
secrets = {
inherit owner;
groups = {
email = [
"personal"
"work"
];
nix = lib.optional cfg.nixSigningKey.enable "signing-key";
};
};
sops = { sops = {
# for yubikey, generate as follows: # for yubikey, generate as follows:
# ``` # ```
# age-plugin-yubikey --identity > <keyfile-path> # age-plugin-yubikey --identity > <keyfile-path>
# ``` # ```
age.keyFile = "/home/${username}/.config/sops/age/keys.txt"; age.keyFile = "/home/${username}/.config/sops/age/keys.txt";
secrets = myUtils.mkSopsSecrets sopsDir owner cfg.groups;
secrets = lib.mkMerge [
(mkSopsSecrets "email" [ "personal" "work" ] { inherit owner; })
(lib.mkIf cfg.nixSigningKey.enable {
nix-signing-key = {
sopsFile = "${sopsDir}/nix.yaml";
key = "signing-key";
inherit owner;
};
})
];
}; };
nix.settings.secret-key-files = lib.mkIf cfg.nixSigningKey.enable [ nix.settings.secret-key-files = lib.mkIf cfg.nixSigningKey.enable [
config.sops.secrets.nix-signing-key.path config.sops.secrets."nix/signing-key".path
]; ];
services = { services = {

View File

@@ -1,19 +1,17 @@
{ config, myUtils, ... }: { config, ... }:
let let
inherit (config.secrets) sopsDir; inherit (config.secrets) owner;
inherit (config.host) username;
owner = config.users.users.${username}.name;
in in
{ {
config.sops = { config = {
secrets = myUtils.mkSopsSecrets sopsDir "taskwarrior" [ secrets.groups.taskwarrior = [
"sync-server-url" "sync-server-url"
"sync-server-client-id" "sync-server-client-id"
"sync-encryption-secret" "sync-encryption-secret"
] { inherit owner; }; ];
templates."taskrc.d/sync" = { sops.templates."taskrc.d/sync" = {
inherit owner; inherit owner;
content = '' content = ''
sync.server.url=${config.sops.placeholder."taskwarrior/sync-server-url"} sync.server.url=${config.sops.placeholder."taskwarrior/sync-server-url"}

View File

@@ -1,41 +1,12 @@
{ lib }: { lib }:
let
hosts = import ./hosts.nix;
secrets = import ./secrets.nix { inherit lib; };
in
{ {
dirNames = dirNames =
path: builtins.attrNames (lib.filterAttrs (_: type: type == "directory") (builtins.readDir path)); path: builtins.attrNames (lib.filterAttrs (_: type: type == "directory") (builtins.readDir path));
hostMeta =
hostDir:
if builtins.pathExists (hostDir + "/meta.nix") then
import (hostDir + "/meta.nix")
else
throw "meta.nix required in ${hostDir}";
mkSopsSecrets =
sopsDir: group: names: extraOpts:
let
file = "${group}.yaml";
in
lib.foldl' lib.mergeAttrs { } (
map (name: {
"${group}/${name}" = {
sopsFile = "${sopsDir}/${file}";
key = name;
}
// extraOpts;
}) names
);
sopsAvailability =
config: osConfig:
let
osSopsAvailable = osConfig != null && osConfig ? sops && osConfig.sops ? secrets;
hmSopsAvailable = config ? sops && config.sops ? secrets;
preferOs = osSopsAvailable;
in
{
available = osSopsAvailable || hmSopsAvailable;
secrets = if preferOs then osConfig.sops.secrets else config.sops.secrets;
templates = if preferOs then osConfig.sops.templates else config.sops.templates;
};
} }
// hosts
// secrets

8
utils/hosts.nix Normal file
View File

@@ -0,0 +1,8 @@
{
hostMeta =
hostDir:
if builtins.pathExists (hostDir + "/meta.nix") then
import (hostDir + "/meta.nix")
else
throw "meta.nix required in ${hostDir}";
}

37
utils/secrets.nix Normal file
View File

@@ -0,0 +1,37 @@
{ lib }:
{
mkSopsSecrets =
sopsDir: owner: groups:
let
opts = lib.optionalAttrs (owner != null) { inherit owner; };
mkGroup =
group: names:
let
file = "${group}.yaml";
in
lib.foldl' lib.mergeAttrs { } (
map (name: {
"${group}/${name}" = {
sopsFile = "${sopsDir}/${file}";
key = name;
}
// opts;
}) names
);
in
lib.foldl' lib.mergeAttrs { } (lib.mapAttrsToList mkGroup groups);
sopsAvailability =
config: osConfig:
let
osSopsAvailable = osConfig != null && osConfig ? sops && osConfig.sops ? secrets;
hmSopsAvailable = config ? sops && config.sops ? secrets;
preferOs = osSopsAvailable;
in
{
available = osSopsAvailable || hmSopsAvailable;
secrets = if preferOs then osConfig.sops.secrets else config.sops.secrets;
templates = if preferOs then osConfig.sops.templates else config.sops.templates;
};
}