{ inputs.nixpkgs.url = "github:NixOS/nixpkgs/a332da8588aeea4feb9359d23f58d95520899e3c"; inputs.flake-utils.url = "github:numtide/flake-utils"; inputs.shell = { type = "path"; path = "./shell"; flake = false; }; inputs.secrets = { type = "path"; path = "./sec"; flake = false; }; inputs.pass = { type = "path"; path = "./pass"; flake = false; }; outputs = flakes: let remoteNixpkgsPatches = [ { meta.description = "dstat: fix pluginpath"; url = "https://github.com/NixOS/nixpkgs/pull/80151.diff"; sha256 = "0jjw2gvp7b7v2n2m2d6yj0gw711j6p9lyjf5ywp2y9ql6905qf4b"; } { meta.description = "syncoid: fix PATH to let it use sudo"; url = "https://github.com/NixOS/nixpkgs/pull/83901.diff"; sha256 = "0q2dicmvl3h3hb9xdd870n5hf6lac489p000c7f1r6k70sh2id4l"; } { meta.description = "sanoid: fix sanoid.conf generation"; url = "https://github.com/NixOS/nixpkgs/pull/83904.diff"; sha256 = "Sy9wPmL+Lfl7hMEeXYOEMk3KlfdL21aL92v6MiGajds="; } { meta.description = "nixos/public-inbox: init"; url = "https://github.com/NixOS/nixpkgs/pull/77450.diff"; sha256 = "13ikg7chpbf6rrg5sngbdb95q3awhdgl4g8vci42xmqyf208hzzd"; } { meta.description = "apparmor: fix and improve the service"; url = "https://github.com/NixOS/nixpkgs/pull/93457.diff"; sha256 = "8QNOKDWNJiZujAvmDtZPZI5VRc+oh40IqjTYn/RCUi4="; } { meta.description = "nixos/security.gnupg: provisioning GnuPG-protected secrets through the Nix store"; url = "https://github.com/NixOS/nixpkgs/pull/93659.diff"; sha256 = "EdofXZ5WspuhKv9K63cbMKWzYPOKWBWVmAG6yRN3gNk="; } { meta.description = "nixos/croc: init"; url = "https://github.com/NixOS/nixpkgs/pull/93629.diff"; sha256 = "0fv3lpj244hvxyixxv4akrr70jv5wwbhb3kmbmd2yskx59a71rch"; } { meta.description = "nixos/biboumi: init"; url = "https://github.com/NixOS/nixpkgs/pull/94917.diff"; sha256 = "1rjmmkx1pm08qi0d0bg6lr60jh8w1ifa8n4gnms4k6wka233yk0z"; } { meta.description = "dovecot_fts_xapian: 1.3.1 -> 1.3.3"; url = "https://github.com/NixOS/nixpkgs/pull/94938.diff"; sha256 = "10bjwcwpvq7nnqdpz0n7c61kb3b27v1abyc80pki7d13jmzzjc04"; } { meta.description = "nixos-install: add support for flakes"; url = "https://github.com/NixOS/nixpkgs/pull/95194.diff"; sha256 = "Fk3AFiY7uRx+8qqX4WvusMYGJ02Xd4PyzLVY5Hzx8gQ="; } ]; localNixpkgsPatches = [ #nixpkgs/patches/security.apparmor.diff #nixpkgs/patches/security.gnupg.diff ]; originPkgs = flakes.nixpkgs.legacyPackages."x86_64-linux"; nixpkgsPath = originPkgs.applyPatches { name = "nixpkgs-patched"; src = flakes.nixpkgs; patches = map originPkgs.fetchpatch remoteNixpkgsPatches ++ localNixpkgsPatches; postPatch = '' patch=$(printf '%s\n' ${builtins.concatStringsSep " " (map (p: p.sha256) remoteNixpkgsPatches ++ localNixpkgsPatches)} | sort | sha256sum | cut -c -7) echo "+patch-$patch" >.version-suffix ''; }; lib = originPkgs.lib; machines = builtins.mapAttrs (machineName: machineConfig: let cfg = import machineConfig { inherit flakes; }; in import (nixpkgsPath + "/nixos/lib/eval-config.nix") (cfg // { extraArgs = { inherit machineName flakes; machines = flakes.self.nixosConfigurations; } // (cfg.extraArgs or {}); modules = cfg.modules ++ [({pkgs, ...}: { system.nixos.versionSuffix = ".${ lib.substring 0 8 (flakes.self.lastModifiedDate or flakes.self.lastModified)}.${ flakes.self.shortRev or "dirty"}"; system.nixos.revision = lib.mkIf (flakes.self ? rev) flakes.self.rev; nix.registry.nixpkgs.flake = nixpkgsPath; nix.package = pkgs.nixFlakes; nix.extraOptions = '' experimental-features = nix-command flakes ''; # Let 'nixos-version --json' know about the Git revision of this flake. system.configurationRevision = lib.mkIf (flakes.self ? rev) flakes.self.rev; /* system.configurationRevision = if flakes.self ? rev then flakes.self.rev else throw "Refusing to build from a dirty Git tree!"; */ })]; })); in { nixosConfigurations = machines { losurdo = machines/losurdo.nix; mermet = machines/mermet.nix; }; } // flakes.flake-utils.lib.eachDefaultSystem (system: #let pkgs = flakes.nixpkgs.legacyPackages.${system}; in let pkgs = import nixpkgsPath { inherit system; config = {}; # Make the config pure, ignoring user's config. overlays = import nixpkgs/overlays.nix; #overlays = import (flakes.self + "/nixpkgs/overlays.nix"); }; in { devShell = import ./shell.nix { inherit flakes pkgs; }; apps = builtins.mapAttrs (machineName: { config, ... }: { type = "app"; program = (let system = config.system.build.toplevel; rootKey = "root/key"; keygrip = builtins.getAttr machineName { losurdo = "9AA84E6F6D71F9163C46BF396B141A0806219077"; mermet = "89F52A879E0019A966503AFFDE72EEA84CDFA3A7"; }; target = "root@${config.networking.hostName}.${config.networking.domain}"; nixCopyFlags = ["--substitute-on-destination"]; profile = "/nix/var/nix/profiles/system"; in pkgs.writeShellScript "install" '' set -eux ssh '${target}' \ ${pkgs.gnupg}/bin/gpg-connect-agent --no-autostart --homedir /var/lib/gnupg "'keyinfo --list'" /bye 2>&1 | grep -qx -e "gpg-connect-agent: no gpg-agent running in this session" \ -e "S KEYINFO ${keygrip} . . . 1 .*" || { # Send the rootKey gpg --decrypt '${config.security.gnupg.store}/${rootKey}.pass.gpg' | gpg --batch --pinentry-mode loopback --passphrase-fd 0 --export-secret-subkeys @root@${machineName} | ssh '${target}' \ gpg --no-autostart --homedir /var/lib/gnupg --no-autostart --batch --pinentry-mode loopback --import # Send the rootKey's passphrase gpg --decrypt '${config.security.gnupg.store}/${rootKey}.pass.gpg' | ssh '${target}' \ gpg-preset-passphrase --homedir /var/lib/gnupg --preset ${keygrip} } ${pkgs.nixFlakes}/bin/nix copy --to ssh://'${target}' \ ${lib.escapeShellArgs nixCopyFlags} ${system} ssh '${target}' \ nix-env --profile '${profile}' --set '${system}' '&&' \ '${profile}'/bin/switch-to-configuration switch '').outPath; }) flakes.self.nixosConfigurations; } ); }