{ # Pin down nixpkgs from github, instead of using global, system or user registries. inputs.nixpkgs.url = "github:NixOS/nixpkgs/6b342809b1b66dce758364f763b64c6a1a9e6211"; #inputs.nixpkgs.url = "github:NixOS/nixpkgs/aee78d463e826ea5c144cff9c8e9e8c02a6ea4a0"; #inputs.nixpkgs.url = "flake:nixpkgs"; 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 = inputs: let remoteNixpkgsPatches = import nixpkgs/patches.nix; localNixpkgsPatches = [ nixpkgs/patches/apparmor.diff #nixpkgs/patches/public-inbox.diff #nixpkgs/patches/zerobin.diff #nixpkgs/patches/gitolite.diff #nixpkgs/patches/tor.diff #nixpkgs/patches/freeciv.diff #nixpkgs/patches/fix-ld-nix.diff #nixpkgs/patches/fix-ld-nix-apparmor.diff ]; originPkgs = inputs.nixpkgs.legacyPackages."x86_64-linux"; nixpkgs = originPkgs.applyPatches { name = "nixpkgs-patched"; src = inputs.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 inputs; }; in import (nixpkgs + "/nixos/lib/eval-config.nix") (cfg // { extraArgs = { inherit machineName inputs; machines = inputs.self.nixosConfigurations; } // (cfg.extraArgs or {}); modules = cfg.modules ++ [({pkgs, ...}: { nix.registry.nixpkgs.flake = nixpkgs; nix.package = pkgs.nixUnstable; nix.extraOptions = "experimental-features = nix-command flakes"; nixpkgs.overlays = import nixpkgs/overlays.nix; system.nixos.versionSuffix = ".${ lib.substring 0 8 (inputs.self.lastModifiedDate or inputs.self.lastModified)}.${ inputs.self.shortRev or "dirty"}"; system.nixos.revision = lib.mkIf (inputs.self ? rev) inputs.self.rev; # Let 'nixos-version --json' know about the Git revision of this flake. system.configurationRevision = lib.mkIf (inputs.self ? rev) inputs.self.rev; boot.initrd.network.ssh.hostKeys = [ "/root/initrd/ssh.key" ]; security.gnupg.agent.enable = true; security.gnupg.store = inputs.pass + "/machines/${machineName}"; /* system.configurationRevision = if inputs.self ? rev then inputs.self.rev else throw "Refusing to build from a dirty Git tree!"; */ programs.ssh.knownHosts = { mermet = { hostNames = [ "mermet" "mermet.sourcephile.fr" ]; publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFvKN2sIpH782MFjaOpcty1Hs/T/TPNJpXI08H3O3oxl"; }; losurdo = { hostNames = [ "losurdo" "losurdo.sourcephile.fr" ]; publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHJkAq1T0Dxozt4RPylvWrUmeuejiG+n/owb3ucnWP9F"; }; }; })]; })); in { # nix -L build .#nixosConfigurations.${machineName}.config.system.build.toplevel nixosConfigurations = machines { losurdo = machines/losurdo.nix; mermet = machines/mermet.nix; }; #nixosModule = import nixos/modules.nix; } // inputs.flake-utils.lib.eachDefaultSystem (system: #let pkgs = inputs.nixpkgs.legacyPackages.${system}; in let pkgs = import nixpkgs { inherit system; overlays = import nixpkgs/overlays.nix; }; in { legacyPackages = pkgs; devShell = import ./shell.nix { inherit inputs pkgs; }; apps = builtins.mapAttrs (machineName: { config, ... }: let system = config.system.build.toplevel; target = "root@${config.networking.hostName}.${config.networking.domain}"; profile = "/nix/var/nix/profiles/system"; in rec { # Example: nix run .#losurdo.switch "switch" = { type = "app"; program = (pkgs.writeShellScript "switch" '' set -eux set -o pipefail nix-store --add-root machines/${machineName}.root --indirect --realise ${system} nix copy --to ssh://'${target}' --substitute-on-destination ${system} ${sendkeys.program} # Send the SSH key of the initrd gpg --decrypt '${config.security.gnupg.store}/initrd/ssh.key.gpg' | ssh '${target}' install -D -m 400 -o root -g root /dev/stdin /root/initrd/ssh.key # Send the Wireguard key of the initrd gpg --decrypt '${config.security.gnupg.store}/wireguard/wg-intra/privateKey.gpg' | ssh '${target}' install -D -m 400 -o root -g root /dev/stdin /root/initrd/wg-intra.key ssh '${target}' \ nix-env --profile '${profile}' --set '${system}' '&&' \ '${profile}'/bin/switch-to-configuration switch '').outPath; }; # Example: nix run .#losurdo.sendkeys "sendkeys" = { type = "app"; program = config.security.gnupg.agent.sendKeys + "/bin/gnupg-agent-sendKeys"; }; }) inputs.self.nixosConfigurations;} ); }