]> Git — Sourcephile - julm/julm-nix.git/blob - flake.nix
nix: try to stop nixos-fallback even if the test fails
[julm/julm-nix.git] / flake.nix
1 {
2 inputs = {
3 doom-emacs.flake = false;
4 doom-emacs.url = "github:hlissner/doom-emacs";
5 home-manager.inputs.nixpkgs.follows = "nixpkgs";
6 home-manager.url = "github:nix-community/home-manager";
7 nix-formatter-pack.inputs.nixpkgs.follows = "nixpkgs";
8 nix-formatter-pack.url = "github:Gerschtli/nix-formatter-pack";
9 nixpkgs.url = "github:NixOS/nixpkgs/fdebb81f45a1ba2c4afca5fd9f526e1653ad0949";
10 pre-commit-hooks.inputs.nixpkgs.follows = "nixpkgs";
11 pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix";
12 };
13
14 outputs = inputs:
15 let
16 remoteNixpkgsPatches = import nixpkgs/patches.nix;
17 localNixpkgsPatches = [
18 ];
19 originPkgs = inputs.nixpkgs.legacyPackages."x86_64-linux";
20 nixpkgsPath = originPkgs.applyPatches {
21 name = "nixpkgs-patched";
22 src = inputs.nixpkgs.outPath;
23 patches = map originPkgs.fetchpatch remoteNixpkgsPatches ++ localNixpkgsPatches;
24 postPatch = ''
25 patch=$(printf '%s\n' ${builtins.concatStringsSep " "
26 (map (p: p.sha256) remoteNixpkgsPatches ++ localNixpkgsPatches)} |
27 sort | sha256sum | cut -c -7)
28 echo "-patch-$patch" >.version-suffix
29 '';
30 };
31 profile = "/nix/var/nix/profiles/system";
32 inherit (inputs.nixpkgs) lib;
33 #nixosSystem = lib.nixosSystem;
34 nixosSystem = import (nixpkgsPath + "/nixos/lib/eval-config.nix");
35 forAllSystems = f: lib.genAttrs lib.systems.flakeExposed (system: f rec {
36 inherit system;
37 #pkgs = inputs.nixpkgs.legacyPackages.${system};
38 pkgs = import nixpkgsPath {
39 inherit system;
40 overlays = import nixpkgs/overlays.nix;
41 };
42 });
43 in
44 {
45 # nix -L build .#nixosConfigurations.oignon.config.system.build.toplevel
46 # nix -L build .#nixosConfigurations.oignon.config.boot.kernelPackages.kernel.configfile
47 # nix -L build .#nixosConfigurations.oignon.pkgs.hello
48 # nix eval --raw .#nixosConfigurations.oignon.config.networking.nftables.rulese
49 nixosConfigurations = lib.genAttrs
50 (builtins.attrNames (lib.filterAttrs (_n: v: v == "directory") (builtins.readDir (inputs.self + "/hosts"))))
51 (hostName: nixosSystem {
52 system = null;
53 specialArgs = {
54 # Required to avoid infinite recursion
55 # when `inputs` is used in `imports`.
56 inherit inputs;
57 };
58 modules = [
59 nixos/default.nix
60 (import (./hosts + "/${hostName}.nix"))
61 {
62 _module.args = {
63 inherit hostName;
64 hosts = inputs.self.nixosConfigurations;
65 host = inputs.self.nixosConfigurations.${hostName}._module.args;
66 };
67 nixpkgs.overlays = import nixpkgs/overlays.nix;
68 nixpkgs.config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [ "hplip" "geogebra" ];
69 }
70 inputs.home-manager.nixosModules.home-manager
71 {
72 home-manager.useGlobalPkgs = true;
73 home-manager.useUserPackages = true;
74 home-manager.verbose = true;
75 home-manager.backupFileExtension = null;
76 home-manager.extraSpecialArgs = {
77 inherit hostName inputs;
78 };
79 }
80 ];
81 });
82
83 # nix -L develop or direnv allow
84 devShell = forAllSystems ({ pkgs, system, ... }: pkgs.callPackage ./shell.nix
85 {
86 inherit pkgs inputs system nixpkgsPath;
87 inherit (inputs.self.checks.${system}.pre-commit-check) shellHook;
88 });
89
90 # nix -L run .#oignon.switch
91 apps = forAllSystems ({ pkgs, system, ... }:
92 with builtins;
93 mapAttrs
94 (hostName: { config, ... }:
95 let
96 inherit (config.system) build;
97 target = "\"\${TARGET:-root@${config.networking.hostName}.${config.networking.domain}}\"";
98 scriptApp = scriptName: ps: script: {
99 type = "app";
100 program = (pkgs.writeShellScript "${hostName}-${scriptName}" ''
101 export PATH="${lib.makeBinPath ([ pkgs.coreutils ] ++ ps)}:$PATH"
102 set -eux
103 ${script}
104 '').outPath;
105 };
106 in
107 {
108 # Example: nix run .#aubergine.switch
109 "switch" = scriptApp "switch" [ ] ''
110 shopt -s globstar
111
112 chmod -R g-rwx,o-rwx **/*.gpg
113 trap 'git reset **/*.gpg' EXIT
114 git rm -rf --cached --ignore-unmatch **/*.gpg # prevent copying to /nix/store
115
116 nix-store --add-root hosts/${hostName}.nixpkgs --indirect --realise ${nixpkgsPath}
117 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.toplevel}
118
119 nix copy --to ssh://${target}${lib.optionalString config.install.substituteOnDestination " --substitute-on-destination"} ${build.toplevel}
120
121 if ssh ${target} set -x ';' \
122 systemctl reset-failed nixos-fallback '2>/dev/null' ';' \
123 systemd-run -u nixos-fallback --description=nixos-fallback /bin/sh -xc '''\'''
124 PATH=${with pkgs; lib.makeBinPath [ coreutils nix systemd ]}
125 sleep $((10 * 60))
126 ${profile}/bin/switch-to-configuration switch
127 systemctl reboot
128 '\'''' '&&' \
129 ${build.toplevel}/bin/switch-to-configuration test
130 then
131 ssh ${target} -o ControlPath=none set -x ';' \
132 systemctl stop nixos-fallback.service ';' \
133 nix-env --profile ${profile} --set '${build.toplevel}' ';' \
134 ${build.toplevel}/bin/switch-to-configuration boot '&&' \
135 nix-env --delete-generations 7d --profile ${profile}
136 else
137 ssh ${target} -o ControlPath=none set -x ';' \
138 systemctl stop nixos-fallback.service
139 false
140 fi
141 '';
142 })
143 inputs.self.nixosConfigurations);
144
145 # nix flake check
146 checks = forAllSystems (args: with args; {
147 pre-commit-check = inputs.pre-commit-hooks.lib.${system}.run {
148 src = inputs.self;
149 hooks = {
150 nixpkgs-fmt.enable = true;
151 };
152 };
153 });
154
155 # nix fmt
156 formatter = forAllSystems ({ pkgs, ... }:
157 inputs.nix-formatter-pack.lib.mkFormatter {
158 inherit pkgs;
159 config = {
160 tools = {
161 deadnix.enable = true;
162 nixpkgs-fmt.enable = true;
163 statix.enable = true;
164 };
165 };
166 }
167 );
168 };
169 }