/
flake.nix
104 lines (88 loc) · 3.54 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
{
description = "jtojnar’s machines";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
};
outputs = { self, nixpkgs }@inputs:
let
inherit (nixpkgs) lib;
# Flakes require ‘packages’ attribute to contain per-platform attrsets.
# Here we explicitly define all the platforms that will be exposed.
platforms = [
"x86_64-linux"
];
forAllPlatforms = f: lib.genAttrs platforms (platform: f platform);
# Generate an attribute set by mapping a function over a list of values.
genAttrs' = values: f: builtins.listToAttrs (map f values);
# Convert a list to file paths to attribute set
# that has the filenames stripped of nix extension as keys
# and imported content of the file as value.
pathsToImportedAttrs = paths:
genAttrs' paths (path: {
name = lib.removeSuffix ".nix" (builtins.baseNameOf path);
value = import path;
});
# Create combined package set from nixpkgs and our overlays.
mkPkgs = platform: import nixpkgs {
system = platform;
overlays = builtins.attrValues self.overlays;
config = { allowUnfree = true; };
};
# Package sets for each platform.
pkgss = forAllPlatforms mkPkgs;
in {
# Configurations for our hosts.
# These are used by tools like nixos-rebuild.
nixosConfigurations =
let
configs = import ./hosts { inherit inputs pkgss; };
in configs;
# Overlay containing our packages defined in this repository.
overlay = import ./common/pkgs;
# All our overlays.
# We will apply them to all hosts.
overlays =
let
overlayDir = ./common/overlays;
fullPath = name: overlayDir + "/${name}";
overlayPaths = map fullPath (builtins.attrNames (builtins.readDir overlayDir));
in pathsToImportedAttrs overlayPaths;
# All our packages listed in /common/pkgs.
packages =
let
# We only have our packages listed in an overlay so we need to extract them from there.
packageAttributes = builtins.attrNames (self.overlay null null);
in
forAllPlatforms (platform: lib.getAttrs packageAttributes pkgss.${platform});
# All our modules and profiles that can be imported.
# A module in /common/modules/baz/qux.nix can be accessed as ‘${flakeRef}.nixosModules.qux’
# A profile in /common/profiles/foo.nix can be accessed as ‘${flakeRef}.nixosModules.profiles.foo’
nixosModules =
let
modulesAttrs = pathsToImportedAttrs (import ./common/modules/list.nix);
profilesAttrs = {
profiles = pathsToImportedAttrs (import ./common/profiles/list.nix);
};
in modulesAttrs // profilesAttrs;
# Development shell containing our maintenance utils
devShell = forAllPlatforms (platform:
pkgss.${platform}.mkShell {
nativeBuildInputs = with pkgss.${platform}; [
deploy
git
nixFlakes
update
];
# Enable flakes even though they are optional
NIX_CONF_DIR = let
current = pkgss.${platform}.lib.optionalString (builtins.pathExists /etc/nix/nix.conf)
(builtins.readFile /etc/nix/nix.conf);
nixConf = pkgss.${platform}.writeTextDir "opt/nix.conf" ''
${current}
experimental-features = nix-command flakes ca-references
'';
in "${nixConf}/opt";
}
);
};
}