]> Git — Sourcephile - julm/julm-nix.git/blob - flake.nix
nix: use latest nixosSystem
[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 nixosConfigurations = lib.genAttrs
46 (builtins.attrNames (lib.filterAttrs (_n: v: v == "directory") (builtins.readDir (inputs.self + "/hosts"))))
47 (hostName: nixosSystem {
48 system = null;
49 specialArgs = {
50 # Required to avoid infinite recursion
51 # when `inputs` is used in `imports`.
52 inherit inputs;
53 };
54 modules = [
55 (import (./hosts + "/${hostName}.nix"))
56 {
57 _module.args = {
58 inherit hostName;
59 hosts = inputs.self.nixosConfigurations;
60 host = inputs.self.nixosConfigurations.${hostName}._module.args;
61 };
62 nixpkgs.overlays = import nixpkgs/overlays.nix;
63 nixpkgs.config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [ "hplip" "geogebra" ];
64 }
65 (import nixos/modules.nix)
66 (import nixos/options.nix)
67 inputs.nixpkgs.nixosModules.notDetected
68 ({ pkgs, ... }: {
69 nix.registry.nixpkgs = lib.mkDefault { flake = inputs.nixpkgs; };
70 nix.package = pkgs.nixFlakes;
71 nix.extraOptions = "experimental-features = nix-command flakes";
72 #nixpkgs.overlays = import nixpkgs/overlays.nix;
73 /*
74 system.nixos.versionSuffix = ".${
75 lib.substring 0 8 (inputs.self.lastModifiedDate or inputs.self.lastModified)}.${
76 inputs.self.shortRev or "dirty"}";
77 system.nixos.revision = lib.mkIf (inputs.self ? rev) inputs.self.rev;
78 */
79 # Let 'nixos-version --json' know about the Git revision of this flake.
80 system.configurationRevision = lib.mkIf (inputs.self ? rev) inputs.self.rev;
81 /*
82 system.configurationRevision =
83 if inputs.self ? rev
84 then inputs.self.rev
85 else throw "Refusing to build from a dirty Git tree!";
86 */
87 })
88 inputs.home-manager.nixosModules.home-manager
89 {
90 home-manager.useGlobalPkgs = true;
91 home-manager.useUserPackages = true;
92 home-manager.verbose = true;
93 home-manager.backupFileExtension = null;
94 home-manager.extraSpecialArgs = {
95 inherit hostName inputs;
96 };
97 }
98 ];
99 });
100 # nix -L develop or direnv allow
101 devShell = forAllSystems ({ pkgs, system, ... }: pkgs.callPackage ./shell.nix
102 {
103 inherit pkgs inputs system nixpkgsPath;
104 inherit (inputs.self.checks.${system}.pre-commit-check) shellHook;
105 });
106 # nix -L run .#oignon.switch
107 apps = forAllSystems ({ pkgs, system, ... }:
108 with builtins;
109 mapAttrs
110 (hostName: { config, ... }:
111 let
112 inherit (config.system) build;
113 target = "\"\${TARGET:-root@${config.networking.hostName}.${config.networking.domain}}\"";
114 scriptApp = scriptName: ps: script: {
115 type = "app";
116 program = (pkgs.writeShellScript "${hostName}-${scriptName}" ''
117 export PATH="${lib.makeBinPath ([ pkgs.coreutils ] ++ ps)}:$PATH"
118 set -eux
119 ${script}
120 '').outPath;
121 };
122 in
123 rec {
124 # Example: nix run .#aubergine.switch
125 "switch" = scriptApp "switch" [ ] ''
126 shopt -s globstar
127
128 chmod -R g-rwx,o-rwx **/*.gpg
129 trap 'git reset **/*.gpg' EXIT
130 git rm -rf --cached --ignore-unmatch **/*.gpg # prevent copying to /nix/store
131
132 nix-store --add-root hosts/${hostName}.nixpkgs --indirect --realise ${nixpkgsPath}
133 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.toplevel}
134
135 nix copy --to ssh://${target}${lib.optionalString config.install.substituteOnDestination " --substitute-on-destination"} ${build.toplevel}
136
137 ssh ${target} set -x ';' \
138 systemctl reset-failed nixos-fallback '2>/dev/null' ';' \
139 systemd-run -u nixos-fallback --description=nixos-fallback /bin/sh -xc '''\'''
140 PATH=${with pkgs; lib.makeBinPath [ coreutils nix systemd ]}
141 sleep $((10 * 60))
142 ${profile}/bin/switch-to-configuration switch
143 systemctl reboot
144 '\'''' '&&' \
145 ${build.toplevel}/bin/switch-to-configuration test
146
147 ssh ${target} -o ControlPath=none set -x ';' \
148 systemctl stop nixos-fallback.service ';' \
149 nix-env --profile ${profile} --set '${build.toplevel}' ';' \
150 ${build.toplevel}/bin/switch-to-configuration boot '&&' \
151 nix-env --delete-generations 7d --profile ${profile}
152 '';
153 })
154 inputs.self.nixosConfigurations);
155 # nix flake check
156 checks = forAllSystems (args: with args; {
157 pre-commit-check = inputs.pre-commit-hooks.lib.${system}.run {
158 src = inputs.self;
159 hooks = {
160 nixpkgs-fmt.enable = true;
161 };
162 };
163 });
164 # nix fmt
165 formatter = forAllSystems ({ pkgs, ... }:
166 inputs.nix-formatter-pack.lib.mkFormatter {
167 inherit pkgs;
168 config = {
169 tools = {
170 deadnix.enable = true;
171 nixpkgs-fmt.enable = true;
172 statix.enable = true;
173 };
174 };
175 }
176 );
177 };
178 }