NixOS deployment for LinuxLab
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.9 KiB

1 week ago
1 week ago
1 week ago
1 week ago
1 week ago
1 week ago
1 week ago
1 week ago
1 week ago
1 week ago
  1. { lib
  2. , runCommandNoCCLocal
  3. , writeText
  4. , ssh-to-age
  5. , machines
  6. , ...
  7. }:
  8. with lib;
  9. let
  10. admins = {
  11. "fooker" = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIK2nkarN0+uSuP5sGwDCb9KRu+FCjO/+da4VypGanPUZ";
  12. };
  13. hosts = {
  14. "nfs" = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMA+Q4wLe0AuZ8OC5BKJLrvmpKcy+6a8Iez9hCSVgtX8";
  15. "ldap" = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFhkh5L4jYl/i4E+lBVDppHcoiohR/gDricyV2wY/3Np";
  16. };
  17. sshToAge = ssh-key:
  18. let
  19. key = runCommandNoCCLocal "hostkey-to-age" { } ''
  20. ${ssh-to-age}/bin/ssh-to-age < '${writeText "" ssh-key}' > "$out"
  21. '';
  22. in
  23. pipe key [
  24. readFile
  25. (removeSuffix "\n")
  26. ];
  27. # Keys for each machine
  28. machine-keys = genAttrs machines (machine:
  29. let
  30. ssh-key = assert assertMsg (hasAttr machine hosts) ''
  31. SSH host key is not specified for machine '${machine}'.
  32. Make sure the SSH host key is added to `sops-config.nix` after initial provisioning.
  33. After changing the hosts, make sure to run `sops updatekeys` with all relevant secret files.
  34. '';
  35. getAttr machine hosts;
  36. in
  37. sshToAge ssh-key);
  38. # Keys for all admins
  39. admin-keys = mapAttrsToList
  40. (_: sshToAge)
  41. admins;
  42. mkRule = path: keys: {
  43. "path_regex" = "^${if path == null then "" else "${escapeRegex path}/"}(${escapeRegex "secrets.yaml"}|secrets/.+)$";
  44. "key_groups" = [{
  45. "age" = keys;
  46. }];
  47. };
  48. # Create a rule for each machine allowing the mechanie and all admins
  49. machine-rules = map
  50. (machine: mkRule
  51. "machines/${machine}"
  52. (admin-keys ++ (singleton machine-keys.${machine})))
  53. machines;
  54. # A single global rule allowing all machines and all admins to access
  55. global-rules = singleton (mkRule null (admin-keys ++ (attrValues machine-keys)));
  56. in
  57. {
  58. inherit admin-keys;
  59. config = {
  60. "creation_rules" = concatLists [
  61. machine-rules
  62. global-rules
  63. ];
  64. };
  65. }