feat(nixos): add sops module for secrets

Adds .sops.yaml file and sops module to nixos to manage secrets.
This commit is contained in:
Mohammad Rafiq 2025-07-07 16:55:37 +08:00
parent d7b8edd054
commit 85f2cee212
No known key found for this signature in database
2 changed files with 59 additions and 0 deletions

7
.sops.yaml Normal file
View file

@ -0,0 +1,7 @@
keys:
- &rafiq age12l33pas8eptwjc7ewux3d8snyzfzwz0tn9qg5kw8le79fswmjgjqdjgyy6
creation_rules:
- path_regex: \.(yaml)$
key_groups:
- age:
- *rafiq

52
nix/modules/secrets.nix Normal file
View file

@ -0,0 +1,52 @@
{
config,
inputs,
lib,
...
}:
let
cfg = config.flake;
inherit (builtins) readFile;
inherit (lib.meta) getExe;
inherit (lib.strings) trim;
inherit (cfg.admin) username pubkey;
in
{
flake.modules.nixos.default =
{ config, ... }:
{
imports = [ inputs.sops-nix.nixosModules.sops ];
config.sops = {
defaultSopsFile = "${cfg.root}/secrets/secrets.yaml";
age.sshKeyPaths = [
"/persist${config.users.defaultUserHome}/${username}/.ssh/id_ed25519"
];
};
};
perSystem =
{ pkgs, ... }:
{
files.files = [
{
path_ = ".sops.yaml";
drv =
pkgs.writeText ".sops.yaml" # yaml
''
keys:
- &${username} ${trim (
readFile "${
pkgs.runCommand "" { } ''
mkdir $out; echo ${pubkey} | ${getExe pkgs.ssh-to-age} > $out/agepubkey
''
}/agepubkey"
)}
creation_rules:
- path_regex: \.(yaml)$
key_groups:
- age:
- *${username}
'';
}
];
};
}