]> Git — Sourcephile - julm/julm-nix.git/blob - flake.nix
nixos-fallback: use a transient service
[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 forAllSystems = f: lib.genAttrs lib.systems.flakeExposed (system: f rec {
35 inherit system;
36 #pkgs = inputs.nixpkgs.legacyPackages.${system};
37 pkgs = import nixpkgsPath {
38 inherit system;
39 config = {
40 allowUnfree = true; # For hplip
41 };
42 overlays = import nixpkgs/overlays.nix;
43 };
44 });
45 nixosSystem = import (nixpkgsPath + "/nixos/lib/eval-config.nix");
46 #nixosSystem = inputs.nixpkgs.lib.nixosSystem;
47 in {
48 nixosConfigurations = lib.genAttrs
49 (builtins.attrNames (lib.filterAttrs (n: v: v == "directory") (builtins.readDir ./hosts)))
50 (hostName: nixosSystem {
51 system = "x86_64-linux";
52 pkgs = import nixpkgsPath {
53 system = "x86_64-linux";
54 config = {
55 allowUnfree = true; # For hplip
56 };
57 overlays = import nixpkgs/overlays.nix;
58 };
59 specialArgs = {
60 inherit hostName inputs;
61 };
62 modules = [
63 {
64 config._module.args = {
65 inherit hostName inputs;
66 hosts = inputs.self.nixosConfigurations;
67 };
68 }
69 (import nixos/modules.nix)
70 (import nixos/options.nix)
71 (import (./hosts + "/${hostName}.nix"))
72 inputs.nixpkgs.nixosModules.notDetected
73 inputs.home-manager.nixosModules.home-manager
74 ({ pkgs, ... }: {
75 nix.registry.nixpkgs = lib.mkDefault { flake = inputs.nixpkgs; };
76 nix.package = pkgs.nixFlakes;
77 nix.extraOptions = "experimental-features = nix-command flakes";
78 #nixpkgs.overlays = import nixpkgs/overlays.nix;
79 /*
80 system.nixos.versionSuffix = ".${
81 lib.substring 0 8 (inputs.self.lastModifiedDate or inputs.self.lastModified)}.${
82 inputs.self.shortRev or "dirty"}";
83 system.nixos.revision = lib.mkIf (inputs.self ? rev) inputs.self.rev;
84 */
85 # Let 'nixos-version --json' know about the Git revision of this flake.
86 system.configurationRevision = lib.mkIf (inputs.self ? rev) inputs.self.rev;
87 /*
88 system.configurationRevision =
89 if inputs.self ? rev
90 then inputs.self.rev
91 else throw "Refusing to build from a dirty Git tree!";
92 */
93 })
94 {
95 home-manager.useGlobalPkgs = true;
96 home-manager.useUserPackages = true;
97 home-manager.verbose = true;
98 home-manager.backupFileExtension = null;
99 home-manager.extraSpecialArgs = {
100 inherit hostName inputs;
101 };
102 }
103 ];
104 });
105 # nix -L develop or direnv allow
106 devShell = forAllSystems ({ pkgs, system, ... }: pkgs.callPackage ./shell.nix
107 {
108 inherit pkgs inputs system nixpkgsPath;
109 #inherit (checks.${system}.pre-commit-check) shellHook;
110 });
111 # nix -L run .#oignon.switch
112 apps = forAllSystems ({ pkgs, system, ... }:
113 with builtins;
114 mapAttrs (hostName: { config, ... }:
115 let
116 build = config.system.build;
117 target = "\"\${TARGET:-root@${config.networking.hostName}.${config.networking.domain}}\"";
118 profile = "/nix/var/nix/profiles/system";
119 nixos-fallback = { delay ? 5 * 60 }: pkgs.writeShellScript "nixos-fallback" ''
120 PATH=${with pkgs; lib.makeBinPath [ coreutils config.nix.package systemd ]}
121 set -eux
122 fallback=$(realpath -e -- ${profile})
123 sleep ${lib.escapeShellArg (toString delay)}
124 echo rollback
125 exit 0
126 nix-env --profile ${profile} --set "$fallback"
127 $fallback/bin/switch-to-configuration switch
128 systemctl reboot
129 '';
130 in rec {
131 # Example: nix run .#aubergine.switch
132 "switch" = {
133 type = "app";
134 program = (pkgs.writeShellScript "switch" (''
135 set -eux
136 set -o pipefail
137 shopt -s globstar
138 chmod -R g-rwx,o-rwx **/*.gpg
139 trap 'git reset **/*.gpg' EXIT
140 git rm -rf --cached --ignore-unmatch **/*.gpg # prevent copying to /nix/store
141
142 nix-store --add-root hosts/${hostName}.nixpkgs --indirect --realise ${nixpkgsPath}
143 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.toplevel}
144 nix copy --to ssh://${target}${lib.optionalString config.install.substituteOnDestination " --substitute-on-destination"} ${build.toplevel}
145 ssh ${target} \
146 systemctl reset-failed nixos-fall '2>/dev/null' ';' \
147 systemd-run -u nixos-fall ${nixos-fallback {}} ';' \
148 ${profile}/bin/switch-to-configuration test
149 ssh ${target} -o ControlPath=none \
150 systemctl stop nixos-fall.service ';' \
151 nix-env --profile ${profile} --set '${build.toplevel}' ';' \
152 ${profile}/bin/switch-to-configuration boot '&&' \
153 nix-env --delete-generations 7d --profile ${profile}
154 ''
155 )).outPath;
156 };
157 }) inputs.self.nixosConfigurations);
158 };
159 }