image: install pngnq
[julm/julm-nix.git] / flake.nix
index 5f8d7a2a4b0757eb4eced9b23572d4f2e7ecef94..695f03494398f0bd83680262d7e903edc5b2793b 100644 (file)
--- a/flake.nix
+++ b/flake.nix
 {
-inputs.nixpkgs.url = "github:NixOS/nixpkgs/495b19d5b3e62b4ec7e846bdfb6ef3d9c3b83492";
-inputs.home-manager.url = "github:nix-community/home-manager";
-inputs.home-manager.inputs.nixpkgs.follows = "nixpkgs";
-inputs.doom-emacs.url = "github:hlissner/doom-emacs";
-inputs.doom-emacs.flake = false;
-/*
-inputs.nix-doom-emacs.url = "github:vlaci/nix-doom-emacs";
-inputs.nix-doom-emacs.inputs.nixpkgs.follows = "nixpkgs";
-inputs.nix-doom-emacs.inputs.home-manager.follows = "home-manager";
-inputs.nix-doom-emacs.inputs.flake-utils.follows = "flake-utils";
-inputs.nix-doom-emacs.inputs.emacs-overlay.follows = "emacs-overlay";
-inputs.emacs-overlay.url = "github:nix-community/emacs-overlay/master";
-*/
-#inputs.pass = { type = "path"; path = "./pass"; flake = false; };
-
-outputs = inputs: let
-  remoteNixpkgsPatches = import nixpkgs/patches.nix;
-  localNixpkgsPatches = [
-  ];
-  originPkgs = inputs.nixpkgs.legacyPackages."x86_64-linux";
-  nixpkgsPath = originPkgs.applyPatches {
-    name = "nixpkgs-patched";
-    src = inputs.nixpkgs.outPath;
-    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
-    '';
+  inputs = {
+    doom-emacs.flake = false;
+    doom-emacs.url = "github:hlissner/doom-emacs";
+    home-manager.inputs.nixpkgs.follows = "nixpkgs";
+    home-manager.url = "github:nix-community/home-manager";
+    nix-formatter-pack.inputs.nixpkgs.follows = "nixpkgs";
+    nix-formatter-pack.url = "github:Gerschtli/nix-formatter-pack";
+    nixpkgs.url = "github:NixOS/nixpkgs/release-22.11";
+    pre-commit-hooks.inputs.nixpkgs.follows = "nixpkgs";
+    pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix";
   };
-  lib = inputs.nixpkgs.lib;
-  profile = "/nix/var/nix/profiles/system";
-  overlays =
-    import nixpkgs/overlays.nix ++
-    [
-      (final: prev: {
-        nixos-fallback = final.writeShellScriptBin "nixos-fallback" ''
-          PATH=${with final; lib.makeBinPath [ coreutils nix systemd ]}
-          set -eux
-          delay=''${1:-300}
-          fallback=$(realpath -e -- ${profile})
-          sleep "$delay"
-          nix-env --profile ${profile} --set "$fallback"
-          $fallback/bin/switch-to-configuration switch
-          systemctl reboot
+
+  outputs = inputs:
+    let
+      remoteNixpkgsPatches = import nixpkgs/patches.nix;
+      localNixpkgsPatches = [
+        nixpkgs/patches/mmsd.diff
+      ];
+      originPkgs = inputs.nixpkgs.legacyPackages."x86_64-linux";
+      nixpkgsPath = originPkgs.applyPatches {
+        name = "nixpkgs-patched";
+        src = inputs.nixpkgs.outPath;
+        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
         '';
-      })
-    ];
-  forAllSystems = f: lib.genAttrs lib.systems.flakeExposed (system: f rec {
-    inherit system;
-    #pkgs = inputs.nixpkgs.legacyPackages.${system};
-    pkgs = import nixpkgsPath {
-      inherit system overlays;
-      config = {
-        allowUnfree = true; # For hplip
       };
-    };
-  });
-  nixosSystem = import (nixpkgsPath + "/nixos/lib/eval-config.nix");
-  #nixosSystem = inputs.nixpkgs.lib.nixosSystem;
-  in {
-    nixosConfigurations = lib.genAttrs
-      (builtins.attrNames (lib.filterAttrs (n: v: v == "directory") (builtins.readDir ./hosts)))
-      (hostName: nixosSystem {
-        system = "x86_64-linux";
+      profile = "/nix/var/nix/profiles/system";
+      inherit (inputs.nixpkgs) lib;
+      #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 {
-          system = "x86_64-linux";
-          config = {
-            allowUnfree = true; # For hplip
-          };
-          inherit overlays;
-        };
-        specialArgs = {
-          inherit hostName inputs;
+          inherit system;
+          overlays = import nixpkgs/overlays.nix;
         };
-        modules = [
-          {
-            config._module.args = {
-              inherit hostName inputs;
-              hosts = inputs.self.nixosConfigurations;
-            };
-          }
-          (import nixos/modules.nix)
-          (import nixos/options.nix)
-          ({pkgs, ...}: {
-            # Send nixos-fallback to the target host
-            environment.systemPackages = [ pkgs.nixos-fallback ];
-          })
-          (import (./hosts + "/${hostName}.nix"))
-          inputs.nixpkgs.nixosModules.notDetected
-          inputs.home-manager.nixosModules.home-manager
-          ({ pkgs, ... }: {
-            nix.registry.nixpkgs = lib.mkDefault { flake = inputs.nixpkgs; };
-            nix.package = pkgs.nixFlakes;
-            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;
-            /*
-            system.configurationRevision =
-              if inputs.self ? rev
-              then inputs.self.rev
-              else throw "Refusing to build from a dirty Git tree!";
-            */
-          })
-          {
-            home-manager.useGlobalPkgs = true;
-            home-manager.useUserPackages = true;
-            home-manager.verbose = true;
-            home-manager.backupFileExtension = null;
-            home-manager.extraSpecialArgs = {
-              inherit hostName inputs;
-            };
-          }
-        ];
-      });
-    # nix -L develop  or  direnv allow
-    devShell = forAllSystems ({ pkgs, system, ... }: pkgs.callPackage ./shell.nix
-      {
-        inherit pkgs inputs system nixpkgsPath;
-        #inherit (checks.${system}.pre-commit-check) shellHook;
       });
-    # nix -L run .#oignon.switch
-    apps = forAllSystems ({ pkgs, system, ... }:
-      with builtins;
-      mapAttrs (hostName: { config, ... }:
-        let
-          build = config.system.build;
-          target = "\"\${TARGET:-root@${config.networking.hostName}.${config.networking.domain}}\"";
-        in rec {
-        # Example: nix run .#aubergine.switch
-        "switch" = {
-          type = "app";
-          program = (pkgs.writeShellScript "switch" (''
-            set -eux
-            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
+    in
+    {
+      # nix -L build .#nixosConfigurations.oignon.config.system.build.toplevel
+      # nix -L build .#nixosConfigurations.oignon.config.boot.kernelPackages.kernel.configfile
+      # nix -L build .#nixosConfigurations.oignon.pkgs.hello
+      # nix eval --raw .#nixosConfigurations.oignon.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
+            (import (./hosts + "/${hostName}.nix"))
+            {
+              _module.args = {
+                inherit hostName;
+                hosts = inputs.self.nixosConfigurations;
+                host = inputs.self.nixosConfigurations.${hostName}._module.args;
+              };
+              nixpkgs.overlays = import nixpkgs/overlays.nix;
+              nixpkgs.config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [ "hplip" "geogebra" ];
+            }
+            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;
+              };
+            }
+          ];
+        });
+
+      # 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;
+        });
+
+      # nix -L run .#oignon.switch
+      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"
+                  set -eux
+                  ${script}
+                '').outPath;
+              };
+            in
+            {
+              # Example: nix run .#aubergine.switch
+              "switch" = scriptApp "switch" [ ] ''
+                shopt -s globstar
 
-            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}
-            ssh ${target} set -x ';' \
-              systemctl reset-failed nixos-fallback '2>/dev/null' ';' \
-              systemd-run -u nixos-fallback ${pkgs.nixos-fallback}/bin/nixos-fallback 300 '&&' \
-              ${profile}/bin/switch-to-configuration test
-            ssh ${target} -o ControlPath=none set -x ';' \
-              systemctl stop nixos-fallback.service ';' \
-              nix-env --profile ${profile} --set '${build.toplevel}' ';' \
-              ${profile}/bin/switch-to-configuration boot '&&' \
-              nix-env --delete-generations 7d --profile ${profile}
-            ''
-          )).outPath;
+                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 copy --to ssh://${target}${lib.optionalString config.install.substituteOnDestination " --substitute-on-destination"} ${build.toplevel}
+
+                if 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
+                then
+                  ssh ${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}
+                else
+                  tput rev
+                  echo WARNING: switch-to-configuration was not registered at boot
+                  tput sgr0
+                  ssh ${target} -o ControlPath=none set -x ';' \
+                    systemctl stop nixos-fallback.service
+                  false
+                fi
+              '';
+            })
+          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;
+          };
         };
-      }) inputs.self.nixosConfigurations);
-  };
+      });
+
+      # 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;
+            };
+          };
+        }
+      );
+    };
 }