-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
flake.nix
264 lines (240 loc) · 9.8 KB
/
flake.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
{
description = ''
Veritas is the personal mono-repo of David Wood; containing the declarative configuration of
servers, desktops and laptops - including dotfiles; a collection of packages; a static site
generator and source of "davidtw.co".
'';
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/release-24.05";
gitignore-nix.url = "github:hercules-ci/gitignore.nix/master";
helix = {
url = "github:helix-editor/helix/master";
inputs.nixpkgs.follows = "nixpkgs";
};
home-manager = {
url = "github:rycee/home-manager/release-24.05";
inputs.nixpkgs.follows = "nixpkgs";
};
rust-overlay = {
url = "github:oxalica/rust-overlay/master";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { self, ... } @ inputs:
with inputs.nixpkgs.lib;
let
forEachSystem = genAttrs [ "x86_64-linux" "aarch64-linux" "aarch64-darwin" ];
pkgsBySystem = forEachSystem (system:
import inputs.nixpkgs {
inherit system;
config = import ./nix/config.nix;
overlays = self.internal.overlays."${system}";
}
);
mkNixOsConfiguration = name: { system, config }:
nameValuePair name (nixosSystem {
inherit system;
modules = [
({ name, ... }: {
# Set the hostname to the name of the configuration being applied (since the
# configuration being applied is determined by the hostname).
networking.hostName = name;
})
({ inputs, ... }: {
# Use the nixpkgs from the flake.
nixpkgs = { pkgs = pkgsBySystem."${system}"; };
# For compatibility with nix-shell, nix-build, etc.
environment.etc.nixpkgs.source = inputs.nixpkgs;
nix.nixPath = [ "nixpkgs=/etc/nixpkgs" ];
})
({ lib, ... }: {
# Set the system configuration revision.
system.configurationRevision = lib.mkIf (self ? rev) self.rev;
})
({ inputs, ... }: {
# Re-expose self and nixpkgs as flakes.
nix.registry = {
self.flake = inputs.self;
nixpkgs = {
from = { id = "nixpkgs"; type = "indirect"; };
flake = inputs.nixpkgs;
};
};
})
(import ./nixos)
(import config)
];
specialArgs = { inherit name inputs; };
});
mkHomeManagerConfiguration = name: { system, config }:
nameValuePair name ({ ... }: {
imports = [
(import ./home)
(import config)
];
# For compatibility with nix-shell, nix-build, etc.
home = {
file.".nixpkgs".source = inputs.nixpkgs;
sessionVariables."NIX_PATH" = "nixpkgs=$HOME/.nixpkgs\${NIX_PATH:+:}$NIX_PATH";
};
# Use the same Nix configuration throughout the system.
xdg.configFile."nixpkgs/config.nix".source = ./nix/config.nix;
# Re-expose self and nixpkgs as flakes.
xdg.configFile."nix/registry.json".text = builtins.toJSON {
version = 2;
flakes =
let
toInput = input:
{
type = "path";
path = input.outPath;
} // (
filterAttrs
(n: _: n == "lastModified" || n == "rev" || n == "revCount" || n == "narHash")
input
);
in
[
{
from = { id = "self"; type = "indirect"; };
to = toInput inputs.self;
}
{
from = { id = "nixpkgs"; type = "indirect"; };
to = toInput inputs.nixpkgs;
}
];
};
});
mkHomeManagerHostConfiguration =
name:
{ configName ? name
, system
, username ? "david"
}:
nameValuePair name (inputs.home-manager.lib.homeManagerConfiguration {
modules = [
(self.internal.homeManagerConfigurations."${configName}")
({ pkgs, ... }: {
home = {
inherit username;
homeDirectory = if pkgs.stdenv.isDarwin then "/Users/${username}" else "/home/${username}";
packages = with pkgs; [
(
# `home-manager` utility does not work with Nix's flakes yet.
writeScriptBin "veritas-switch-config" ''
#! ${runtimeShell} -e
nix --version
nix build ".#homeManagerConfigurations.${configName}.activationPackage" $@
./result/activate
''
)
];
};
xdg.configFile."nix/nix.conf".text =
let
nixConf = import ./nix/conf.nix;
substituters = [ "https://cache.nixos.org" ] ++ nixConf.binaryCaches;
trustedPublicKeys = [
"cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY="
] ++ nixConf.binaryCachePublicKeys;
in
''
experimental-features = nix-command flakes
substituters = ${builtins.concatStringsSep " " substituters}
trusted-public-keys = ${builtins.concatStringsSep " " trustedPublicKeys}
'';
nixpkgs = {
config = import ./nix/config.nix;
overlays = self.internal.overlays."${system}";
};
})
];
pkgs = pkgsBySystem."${system}";
});
in
{
# `internal` isn't a known output attribute for flakes. It is used here to contain
# anything that isn't meant to be re-usable.
internal = {
# Expose the development shells defined in the repository, run these with:
#
# nix dev-shell 'self#devShells.x86_64-linux.rustc'
devShells = forEachSystem (system: { });
# Attribute set of hostnames to home-manager modules with the entire configuration for
# that host - consumed by the home-manager NixOS module for that host (if it exists)
# or by `mkHomeManagerHostConfiguration` for home-manager-only hosts.
homeManagerConfigurations = mapAttrs' mkHomeManagerConfiguration {
dtw-macbook-air = { system = "aarch64-darwin"; config = ./home/hosts/macbook-air.nix; };
};
# Overlays consumed by the home-manager/NixOS configuration.
overlays = forEachSystem (system: [
(self.overlay."${system}")
(inputs.rust-overlay.overlays.default)
(inputs.gitignore-nix.overlay)
(_: _: {
# Make unstable version of Helix default.
helix = inputs.helix.packages."${system}".helix;
})
(import ./nix/overlays/iosevka.nix)
]);
# Expose the static site generator as a re-usable library of sorts - it definitely isn't
# a recognized output by Nix and there's no stability guarantees.
staticSiteGenerator = forEachSystem (system:
import ./web/lib { inherit system; pkgs = pkgsBySystem."${system}"; }
);
# Expose any websites built by the static site generator as outputs too.
staticSites = forEachSystem (system:
let
pkgs = pkgsBySystem."${system}";
site = self.internal.staticSiteGenerator."${system}";
in
{
"davidtwco" = import ./web/src { inherit pkgs site; };
}
);
};
# Attribute set of hostnames to evaluated home-manager configurations.
homeManagerConfigurations = mapAttrs' mkHomeManagerHostConfiguration {
dtw-macbook-air = { system = "aarch64-darwin"; username = "davidtw"; };
};
# Attribute set of hostnames to evaluated NixOS configurations. Consumed by `nixos-rebuild`
# on those hosts.
nixosConfigurations = mapAttrs' mkNixOsConfiguration { };
# Import the modules exported by this flake. Explicitly don't expose profiles in
# `nixos/configs` and `nixos/profiles` - these are only used for internal organization
# of my configurations.
#
# These are only used by other projects that might import this flake.
nixosModules = { };
# Expose a dev shell which contains tools for working on this repository.
devShell = forEachSystem (system:
with pkgsBySystem."${system}";
mkShell {
name = "veritas";
buildInputs = [ ];
}
);
# Expose an overlay which provides the packages defined by this repository.
#
# Overlays are used more widely in this repository, but often for modifying upstream packages
# or making third-party packages easier to access - it doesn't make sense to share those,
# so they in the flake output `internal.overlays`.
#
# These are meant to be consumed by other projects that might import this flake.
overlay = forEachSystem (system: _: _: self.packages."${system}");
# Expose the packages defined in this flake, built for any supported systems. These are
# meant to be consumed by other projects that might import this flake.
packages = forEachSystem (system:
let
pkgs = pkgsBySystem."${system}";
in
{
measureme = pkgs.callPackage ./nix/packages/measureme { };
rustfilt = pkgs.callPackage ./nix/packages/rustfilt { };
tera-template = pkgs.callPackage ./nix/packages/tera-template { };
}
);
defaultPackage = forEachSystem (system: self.internal.staticSites."${system}"."davidtwco");
};
}