Skip to content

Instantly share code, notes, and snippets.

@clamydo
Created April 4, 2024 09:17
Show Gist options
  • Save clamydo/9691c48552efcd6d338407d58c900a4a to your computer and use it in GitHub Desktop.
Save clamydo/9691c48552efcd6d338407d58c900a4a to your computer and use it in GitHub Desktop.
Minimal example how to pass a sops-nix secret into a systemd-nspawn container via systemd's credential system.
{ config, lib, pkgs, ... }:
{
imports = [
${builtins.fetchTarball
"https://github.com/Mic92/sops-nix/archive/master.tar.gz"
}/modules/sops"
];
users = {
mutableUsers = true;
users."nixos" = {
isNormalUser = true;
home = "/home/nixos";
password = "nixos";
extraGroups = [ "wheel" ];
};
};
sops.defaultSopsFile = ./secrets.yaml;
sops.age.keyFile = ./key.txt;
sops.age.generateKey = false;
sops.secrets.example_key = { };
containers.test = {
autoStart = true;
extraFlags = [
"--load-credential=examplekey:${config.sops.secrets.example_key.path}"
];
config = {
system.stateVersion = "23.11";
systemd.services.foobar = {
enable = true;
script = ''
echo $CREDENTIALS_DIRECTORY
cat $CREDENTIALS_DIRECTORY/examplekeypropageted
# will fail, no access
cat $CREDENTIALS_DIRECTORY/examplekey
'';
serviceConfig = { LoadCredential = "examplekeypropageted:examplekey"; };
wantedBy = [ "multi-user.target" ];
};
};
};
system.stateVersion = "23.11";
}
age-keygen -o key.txt
# setup .sops.yaml
env SOPS_AGE_KEY_FILE=key.txt sops secrets.yaml
# add `example_key: mysecret`
nixos-rebuild -I nixos-config=configuration.nix build-vm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment