]> Git — Sourcephile - julm/julm-nix.git/blob - flake.nix
nixos-fallback: make sure the script exists on the remote
[julm/julm-nix.git] / flake.nix
1 {
2 inputs.nixpkgs.url = "github:NixOS/nixpkgs/495b19d5b3e62b4ec7e846bdfb6ef3d9c3b83492";
3 inputs.home-manager.url = "github:nix-community/home-manager";
4 inputs.home-manager.inputs.nixpkgs.follows = "nixpkgs";
5 inputs.doom-emacs.url = "github:hlissner/doom-emacs";
6 inputs.doom-emacs.flake = false;
7 /*
8 inputs.nix-doom-emacs.url = "github:vlaci/nix-doom-emacs";
9 inputs.nix-doom-emacs.inputs.nixpkgs.follows = "nixpkgs";
10 inputs.nix-doom-emacs.inputs.home-manager.follows = "home-manager";
11 inputs.nix-doom-emacs.inputs.flake-utils.follows = "flake-utils";
12 inputs.nix-doom-emacs.inputs.emacs-overlay.follows = "emacs-overlay";
13 inputs.emacs-overlay.url = "github:nix-community/emacs-overlay/master";
14 */
15 #inputs.pass = { type = "path"; path = "./pass"; flake = false; };
16
17 outputs = inputs: let
18 remoteNixpkgsPatches = import nixpkgs/patches.nix;
19 localNixpkgsPatches = [
20 ];
21 originPkgs = inputs.nixpkgs.legacyPackages."x86_64-linux";
22 nixpkgsPath = originPkgs.applyPatches {
23 name = "nixpkgs-patched";
24 src = inputs.nixpkgs.outPath;
25 patches = map originPkgs.fetchpatch remoteNixpkgsPatches ++ localNixpkgsPatches;
26 postPatch = ''
27 patch=$(printf '%s\n' ${builtins.concatStringsSep " "
28 (map (p: p.sha256) remoteNixpkgsPatches ++ localNixpkgsPatches)} |
29 sort | sha256sum | cut -c -7)
30 echo "-patch-$patch" >.version-suffix
31 '';
32 };
33 lib = inputs.nixpkgs.lib;
34 profile = "/nix/var/nix/profiles/system";
35 overlays =
36 import nixpkgs/overlays.nix ++
37 [
38 (final: prev: {
39 nixos-fallback = final.writeShellScriptBin "nixos-fallback" ''
40 PATH=${with final; lib.makeBinPath [ coreutils nix systemd ]}
41 set -eux
42 delay=''${1:-300}
43 fallback=$(realpath -e -- ${profile})
44 sleep "$delay"
45 nix-env --profile ${profile} --set "$fallback"
46 $fallback/bin/switch-to-configuration switch
47 systemctl reboot
48 '';
49 })
50 ];
51 forAllSystems = f: lib.genAttrs lib.systems.flakeExposed (system: f rec {
52 inherit system;
53 #pkgs = inputs.nixpkgs.legacyPackages.${system};
54 pkgs = import nixpkgsPath {
55 inherit system overlays;
56 config = {
57 allowUnfree = true; # For hplip
58 };
59 };
60 });
61 nixosSystem = import (nixpkgsPath + "/nixos/lib/eval-config.nix");
62 #nixosSystem = inputs.nixpkgs.lib.nixosSystem;
63 in {
64 nixosConfigurations = lib.genAttrs
65 (builtins.attrNames (lib.filterAttrs (n: v: v == "directory") (builtins.readDir ./hosts)))
66 (hostName: nixosSystem {
67 system = "x86_64-linux";
68 pkgs = import nixpkgsPath {
69 system = "x86_64-linux";
70 config = {
71 allowUnfree = true; # For hplip
72 };
73 inherit overlays;
74 };
75 specialArgs = {
76 inherit hostName inputs;
77 };
78 modules = [
79 {
80 config._module.args = {
81 inherit hostName inputs;
82 hosts = inputs.self.nixosConfigurations;
83 };
84 }
85 (import nixos/modules.nix)
86 (import nixos/options.nix)
87 ({pkgs, ...}: {
88 # Send nixos-fallback to the target host
89 environment.systemPackages = [ pkgs.nixos-fallback ];
90 })
91 (import (./hosts + "/${hostName}.nix"))
92 inputs.nixpkgs.nixosModules.notDetected
93 inputs.home-manager.nixosModules.home-manager
94 ({ pkgs, ... }: {
95 nix.registry.nixpkgs = lib.mkDefault { flake = inputs.nixpkgs; };
96 nix.package = pkgs.nixFlakes;
97 nix.extraOptions = "experimental-features = nix-command flakes";
98 #nixpkgs.overlays = import nixpkgs/overlays.nix;
99 /*
100 system.nixos.versionSuffix = ".${
101 lib.substring 0 8 (inputs.self.lastModifiedDate or inputs.self.lastModified)}.${
102 inputs.self.shortRev or "dirty"}";
103 system.nixos.revision = lib.mkIf (inputs.self ? rev) inputs.self.rev;
104 */
105 # Let 'nixos-version --json' know about the Git revision of this flake.
106 system.configurationRevision = lib.mkIf (inputs.self ? rev) inputs.self.rev;
107 /*
108 system.configurationRevision =
109 if inputs.self ? rev
110 then inputs.self.rev
111 else throw "Refusing to build from a dirty Git tree!";
112 */
113 })
114 {
115 home-manager.useGlobalPkgs = true;
116 home-manager.useUserPackages = true;
117 home-manager.verbose = true;
118 home-manager.backupFileExtension = null;
119 home-manager.extraSpecialArgs = {
120 inherit hostName inputs;
121 };
122 }
123 ];
124 });
125 # nix -L develop or direnv allow
126 devShell = forAllSystems ({ pkgs, system, ... }: pkgs.callPackage ./shell.nix
127 {
128 inherit pkgs inputs system nixpkgsPath;
129 #inherit (checks.${system}.pre-commit-check) shellHook;
130 });
131 # nix -L run .#oignon.switch
132 apps = forAllSystems ({ pkgs, system, ... }:
133 with builtins;
134 mapAttrs (hostName: { config, ... }:
135 let
136 build = config.system.build;
137 target = "\"\${TARGET:-root@${config.networking.hostName}.${config.networking.domain}}\"";
138 in rec {
139 # Example: nix run .#aubergine.switch
140 "switch" = {
141 type = "app";
142 program = (pkgs.writeShellScript "switch" (''
143 set -eux
144 set -o pipefail
145 shopt -s globstar
146 chmod -R g-rwx,o-rwx **/*.gpg
147 trap 'git reset **/*.gpg' EXIT
148 git rm -rf --cached --ignore-unmatch **/*.gpg # prevent copying to /nix/store
149
150 nix-store --add-root hosts/${hostName}.nixpkgs --indirect --realise ${nixpkgsPath}
151 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.toplevel}
152 nix copy --to ssh://${target}${lib.optionalString config.install.substituteOnDestination " --substitute-on-destination"} ${build.toplevel}
153 ssh ${target} set -x ';' \
154 systemctl reset-failed nixos-fallback '2>/dev/null' ';' \
155 systemd-run -u nixos-fallback ${pkgs.nixos-fallback}/bin/nixos-fallback 300 '&&' \
156 ${profile}/bin/switch-to-configuration test
157 ssh ${target} -o ControlPath=none set -x ';' \
158 systemctl stop nixos-fallback.service ';' \
159 nix-env --profile ${profile} --set '${build.toplevel}' ';' \
160 ${profile}/bin/switch-to-configuration boot '&&' \
161 nix-env --delete-generations 7d --profile ${profile}
162 ''
163 )).outPath;
164 };
165 }) inputs.self.nixosConfigurations);
166 };
167 }