mermet: nebula: sourcephile.fr: adapt to new conventions
[sourcephile-nix.git] / flake.nix
index ee2124da315c47ed3032052c195a7d949e7a9829..33265194099658fe0bb562f8e9bd22c4f249d104 100644 (file)
--- a/flake.nix
+++ b/flake.nix
@@ -2,14 +2,15 @@
   inputs = {
     doom-emacs.follows = "julm-nix/doom-emacs";
     home-manager.follows = "julm-nix/home-manager";
-    julm-nix.url = "git+file:///home/julm/work/sourcephile/nix/julm-nix?ref=main";
     julm-nix.inputs.nixpkgs.follows = "nixpkgs";
-    nix-formatter-pack.follows = "julm-nix/nix-formatter-pack";
-    nixpkgs.url = "github:NixOS/nixpkgs/release-22.11";
-    pre-commit-hooks.follows = "julm-nix/pre-commit-hooks";
+    julm-nix.url = "git+file:///home/julm/work/sourcephile/nix/julm-nix?ref=main";
+    #nixpkgs.follows = "julm-nix/nixpkgs";
+    nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11";
+    git-hooks.follows = "julm-nix/git-hooks";
   };
 
-  outputs = inputs:
+  outputs =
+    inputs:
     let
       remoteNixpkgsPatches = import (inputs.julm-nix + "/nixpkgs/patches.nix");
       localNixpkgsPatches = [
         src = inputs.nixpkgs;
         patches = map originPkgs.fetchpatch remoteNixpkgsPatches ++ localNixpkgsPatches;
         postPatch = ''
-          patch=$(printf '%s\n' ${builtins.concatStringsSep " "
-             (map (p: p.sha256) remoteNixpkgsPatches ++ localNixpkgsPatches)} |
+          patch=$(printf '%s\n' ${
+            builtins.concatStringsSep " " (map (p: p.sha256) remoteNixpkgsPatches ++ localNixpkgsPatches)
+          } |
             sort | sha256sum | cut -c -7)
           echo "-patch-$patch" >.version-suffix
         '';
       };
       profile = "/nix/var/nix/profiles/system";
       inherit (inputs.nixpkgs) lib;
+      overlays =
+        system:
+        [
+          (finalPkgs: previousPkgs: { })
+        ]
+        ++ import (inputs.julm-nix + "/nixpkgs/overlays.nix")
+        ++ import nixpkgs/overlays.nix;
       #nixosSystem = lib.nixosSystem;
       nixosSystem = import (nixpkgsPath + "/nixos/lib/eval-config.nix");
-      forAllSystems = f: lib.genAttrs lib.systems.flakeExposed (system: f rec {
-        inherit system;
-        #pkgs = inputs.nixpkgs.legacyPackages.${system};
-        pkgs = import nixpkgsPath {
-          inherit system;
-          overlays =
-            import nixpkgs/overlays.nix ++
-            import (inputs.julm-nix + "/nixpkgs/overlays.nix");
-        };
-      });
+      forAllSystems =
+        f:
+        lib.genAttrs lib.systems.flakeExposed (
+          system:
+          f rec {
+            inherit system;
+            #pkgs = inputs.nixpkgs.legacyPackages.${system};
+            pkgs = import nixpkgsPath {
+              inherit system;
+              overlays = overlays system;
+            };
+          }
+        );
     in
     {
+      # nix -L build .#hello
+      packages = forAllSystems (args: with args; pkgs);
+
       # nix -L build .#nixosConfigurations.mermet.config.system.build.toplevel
       # nix -L build .#nixosConfigurations.mermet.config.boot.kernelPackages.kernel.configfile
       # nix -L build .#nixosConfigurations.mermet.pkgs.hello
-      # nix eval --raw .#nixosConfigurations.mermet.config.networking.nftables.rulese
-      nixosConfigurations = lib.genAttrs
-        (builtins.attrNames (lib.filterAttrs (_n: v: v == "directory") (builtins.readDir (inputs.self + "/hosts"))))
-        (hostName: nixosSystem
-          {
-            system = null;
-            specialArgs = {
-              # Required to avoid infinite recursion
-              # when `inputs` is used in `imports`.
-              inherit inputs;
-            };
-            modules = [
-              nixos/default.nix
-              (inputs.self + "/hosts/${hostName}.nix")
-              {
-                _module.args = {
-                  inherit hostName;
-                  hosts = inputs.self.nixosConfigurations;
-                  host = inputs.self.nixosConfigurations.${hostName}._module.args;
-                };
-                nixpkgs.overlays =
-                  import nixpkgs/overlays.nix ++
-                  import (inputs.julm-nix + "/nixpkgs/overlays.nix");
-                #nixpkgs.buildPlatform = "x86_64-linux";
-                nixpkgs.config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [ "hplip" ];
-              }
-              inputs.nixpkgs.nixosModules.notDetected
-              inputs.home-manager.nixosModules.home-manager
-              {
-                home-manager.useGlobalPkgs = true;
-                home-manager.useUserPackages = true;
-                home-manager.verbose = true;
-                home-manager.backupFileExtension = null;
-                home-manager.extraSpecialArgs = {
-                  inherit hostName inputs;
-                };
-              }
-              {
-                programs.ssh.knownHosts = {
-                  carotte = {
-                    extraHostNames = [ "carotte" "carotte.sourcephile.fr" ];
-                    publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKnnS0w9zm5KuWwlfJu+qEXC0asESQySPp76szyMTE3J";
-                  };
-                  mermet = {
-                    extraHostNames = [ "mermet" "mermet.sourcephile.fr" ];
-                    publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFvKN2sIpH782MFjaOpcty1Hs/T/TPNJpXI08H3O3oxl";
+      # nix eval --raw .#nixosConfigurations.mermet.config.networking.nftables.ruleset
+      nixosConfigurations =
+        lib.genAttrs
+          (builtins.attrNames (
+            lib.filterAttrs (_n: v: v == "directory") (builtins.readDir (inputs.self + "/hosts"))
+          ))
+          (
+            hostName:
+            nixosSystem {
+              system = null;
+              specialArgs = {
+                # Required to avoid infinite recursion
+                # when `inputs` is used in `imports`.
+                inherit inputs;
+              };
+              modules = [
+                nixos/default.nix
+                (inputs.julm-nix + "/nixos/default.nix")
+                (inputs.self + "/hosts/${hostName}.nix")
+                (
+                  { config, ... }:
+                  {
+                    _module.args = {
+                      inherit hostName;
+                      hosts = inputs.self.nixosConfigurations;
+                      host = inputs.self.nixosConfigurations.${hostName}._module.args;
+                      info = import ./info.nix;
+                    };
+                    nixpkgs.overlays = overlays config.nixpkgs.hostPlatform.system;
+                    #nixpkgs.buildPlatform = "x86_64-linux";
+                    nixpkgs.config.allowUnfreePredicate =
+                      pkg:
+                      builtins.elem (lib.getName pkg) [
+                        "canon-cups-ufr2"
+                        "hplip"
+                      ];
+                  }
+                )
+                inputs.nixpkgs.nixosModules.notDetected
+                inputs.home-manager.nixosModules.home-manager
+                {
+                  home-manager.useGlobalPkgs = true;
+                  home-manager.useUserPackages = true;
+                  home-manager.verbose = true;
+                  home-manager.backupFileExtension = null;
+                  home-manager.extraSpecialArgs = {
+                    inherit hostName inputs;
                   };
-                  losurdo = {
-                    extraHostNames = [ "losurdo" "losurdo.sourcephile.fr" ];
-                    publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHJkAq1T0Dxozt4RPylvWrUmeuejiG+n/owb3ucnWP9F";
+                }
+                {
+                  programs.ssh.knownHosts = {
+                    carotte = {
+                      extraHostNames = [
+                        "carotte"
+                        "carotte.sourcephile.fr"
+                      ];
+                      publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKnnS0w9zm5KuWwlfJu+qEXC0asESQySPp76szyMTE3J";
+                    };
+                    mermet = {
+                      extraHostNames = [
+                        "mermet"
+                        "mermet.sourcephile.fr"
+                      ];
+                      publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFvKN2sIpH782MFjaOpcty1Hs/T/TPNJpXI08H3O3oxl";
+                    };
+                    losurdo = {
+                      extraHostNames = [
+                        "losurdo"
+                        "losurdo.sourcephile.fr"
+                      ];
+                      publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHJkAq1T0Dxozt4RPylvWrUmeuejiG+n/owb3ucnWP9F";
+                    };
                   };
-                };
-              }
-            ];
-          }
-        );
+                }
+              ];
+            }
+          );
 
       # nix -L develop  or  direnv allow
-      devShell = forAllSystems ({ pkgs, system, ... }: pkgs.callPackage ./shell.nix
-        {
-          inherit pkgs inputs system nixpkgsPath;
-          inherit (inputs.self.checks.${system}.pre-commit-check) shellHook;
-        });
+      devShell = forAllSystems (
+        { pkgs, system, ... }:
+        pkgs.callPackage ./shell.nix {
+          inherit
+            pkgs
+            inputs
+            system
+            nixpkgsPath
+            ;
+          inherit (inputs.self.checks.${system}.git-hooks-check) shellHook;
+        }
+      );
 
       # nix -L run .#mermet.switch
-      apps = forAllSystems ({ pkgs, system, ... }:
+      apps = forAllSystems (
+        { pkgs, system, ... }:
         with builtins;
-        mapAttrs
-          (hostName: { config, ... }:
-            let
-              inherit (config.system) build;
-              target = "\"\${TARGET:-root@${config.networking.hostName}.${config.networking.domain}}\"";
-              scriptApp = scriptName: ps: script: {
-                type = "app";
-                program = (pkgs.writeShellScript "${hostName}-${scriptName}" ''
-                  export PATH="${lib.makeBinPath ([ pkgs.coreutils ] ++ ps)}:$PATH"
+        mapAttrs (
+          hostName:
+          { config, pkgs, ... }:
+          let
+            inherit (config.system) build;
+            scriptApp = scriptName: ps: script: {
+              type = "app";
+              program =
+                (pkgs.writeShellScript "${hostName}-${scriptName}" ''
+                  export PATH="${lib.makeBinPath ([ pkgs.buildPackages.coreutils ] ++ ps)}:$PATH"
                   set -eux
                   ${script}
                 '').outPath;
-              };
-            in
-            {
-              # Example: nix run .#mermet.switch
-              "switch" = scriptApp "switch" [ ] (''
-                set -o pipefail
-                shopt -s globstar
+            };
+          in
+          {
+            # Example: nix run .#mermet.switch
+            "switch" = scriptApp "switch" [ ] (''
+              set -o pipefail
+              shopt -s globstar
 
-                chmod -R g-rwx,o-rwx **/*.gpg
-                trap 'git reset **/*.gpg' EXIT
-                git rm -rf --cached --ignore-unmatch **/*.gpg # prevent copying to /nix/store
+              chmod -R g-rwx,o-rwx **/*.gpg
+              trap 'git reset **/*.gpg' EXIT
+              git rm -rf --cached --ignore-unmatch **/*.gpg # prevent copying to /nix/store
 
-                nix-store --add-root hosts/${hostName}.nixpkgs --indirect --realise ${nixpkgsPath}
-                nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.toplevel}
+              nix-store --add-root hosts/${hostName}.nixpkgs --indirect --realise ${nixpkgsPath}
+              nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.toplevel}
 
-                nix copy --to ssh://${target}${lib.optionalString config.install.substituteOnDestination " --substitute-on-destination"} ${build.toplevel}
-              '' +
-              # TODO: make it a LoadCredentialEncrypted= in nix-daemon.service
-              lib.optionalString config.boot.initrd.network.ssh.enable ''
-                # Send the SSH key of the initrd
-                gpg --decrypt 'hosts/${hostName}/ssh/initrd.key.gpg' |
-                ssh ${target} install -D -m 400 -o root -g root /dev/stdin /root/initrd/ssh.key
-              '' + ''
-                ssh ${target} set -x ';' \
-                  systemctl reset-failed nixos-fallback '2>/dev/null' ';' \
-                  test "''${NO_NIXOS_FALLBACK:+set}" '||' systemd-run -u nixos-fallback --description=nixos-fallback /bin/sh -xc '''\'''
-                    PATH=${with pkgs; lib.makeBinPath [ coreutils nix systemd ]}
-                    sleep $((10 * 60))
-                    ${profile}/bin/switch-to-configuration switch
-                    systemctl reboot
-                  '\'''' '&&' \
-                  ${build.toplevel}/bin/switch-to-configuration test
+              nix copy --to ssh://${config.install.target}${lib.optionalString config.install.substituteOnDestination " --substitute-on-destination"} ${build.toplevel}
 
-                ssh ${target} -o ControlPath=none set -x ';' \
+              ${config.security.initrd.install}
+
+              if ssh ${config.install.target} set -x ';' \
+                systemctl reset-failed nixos-fallback '2>/dev/null' ';' \
+                test "''${NO_NIXOS_FALLBACK:+set}" '||' \
+                systemd-run -u nixos-fallback --description=nixos-fallback /bin/sh -xc '''\'''
+                  PATH=${
+                    with pkgs;
+                    lib.makeBinPath [
+                      coreutils
+                      nix
+                      systemd
+                    ]
+                  }
+                  sleep $((10 * 60))
+                  ${profile}/bin/switch-to-configuration switch
+                  systemctl reboot
+                '\'''' '&&' \
+                ${build.toplevel}/bin/switch-to-configuration test
+              then
+                ssh ${config.install.target} -o ControlPath=none set -x ';' \
                   systemctl stop nixos-fallback.service ';' \
                   nix-env --profile ${profile} --set '${build.toplevel}' ';' \
                   ${build.toplevel}/bin/switch-to-configuration boot '&&' \
                   nix-env --delete-generations 7d --profile ${profile}
-              '');
-              # Example: nix run .#carotte.install-sd
-              # DELETEME: quite useless, better to use nixos-install
-              "install-sd" = scriptApp "install-sd" [ zstd ] ''
-                set -o pipefail
-                nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.sdImage}
-                unzstd --stdout ${build.sdImage}/sd-image/*.img.zst |
-                sudo -k dd conv=notrunc oflag=direct,sync status=progress of="''${1:-/dev/mmcblk0}"
-              '';
-            }
-          )
-          inputs.self.nixosConfigurations
+              else
+                tput rev
+                echo WARNING: switch-to-configuration was not registered at boot
+                tput sgr0
+                ssh ${config.install.target} -o ControlPath=none set -x ';' \
+                  systemctl stop nixos-fallback.service
+                false
+              fi
+            '');
+            # Example: nix run .#carotte.install-sd
+            # DELETEME: quite useless, better to use nixos-install
+            "install-sd" = scriptApp "install-sd" [ pkgs.zstd ] ''
+              set -o pipefail
+              nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.sdImage}
+              unzstd --stdout ${build.sdImage}/sd-image/*.img.zst |
+              sudo -k dd conv=notrunc oflag=direct,sync status=progress of="''${1:-/dev/mmcblk0}"
+            '';
+          }
+        ) inputs.self.nixosConfigurations
       );
 
       # nix flake check
-      checks = forAllSystems (args: with args; {
-        pre-commit-check = inputs.pre-commit-hooks.lib.${system}.run {
-          src = inputs.self;
-          hooks = {
-            nixpkgs-fmt.enable = true;
-          };
-        };
-      });
-
-      # nix fmt
-      formatter = forAllSystems ({ pkgs, ... }:
-        inputs.nix-formatter-pack.lib.mkFormatter {
-          inherit pkgs;
-          config = {
-            tools = {
-              deadnix.enable = true;
-              nixpkgs-fmt.enable = true;
-              statix.enable = true;
+      checks = forAllSystems (
+        args: with args; {
+          git-hooks-check = inputs.git-hooks.lib.${system}.run {
+            src = inputs.self;
+            hooks = {
+              nixfmt-rfc-style.enable = true;
             };
           };
         }