-
Notifications
You must be signed in to change notification settings - Fork 2
/
flake.nix
159 lines (130 loc) · 3.9 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
{
description = "Example NixOS deployment";
# nixConfig = {
# extra-substituters = [ "https://example.cachix.org" ];
# extra-trusted-public-keys = [ "example.cachix.org-1:xxxx=" ];
# };
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
agenix = {
url = "github:ryantm/agenix/0.15.0";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = inputs@{ self, nixpkgs, agenix }:
let
inherit (self) outputs;
# Flake system
supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
nixpkgsFor = forAllSystems (system: import nixpkgs {
inherit system;
config.allowUnfree = true;
overlays = [ self.overlays.default ];
});
# Version
stateVersion = "24.11";
srcRevision = if self ? rev then self.rev else "dirty";
systemMeta = {
configurationRevision = srcRevision;
stateVersion = stateVersion;
};
# Make NixOS server host
mkHost = hostname: nixpkgs.lib.nixosSystem {
modules = [
./hosts/${hostname}
agenix.nixosModules.default
];
specialArgs = {
inherit inputs outputs hostname systemMeta;
};
};
# Make NixOS VM
mkVm = hostname: {
type = "app";
program = "${self.nixosConfigurations.${hostname}.config.system.build.vm}/bin/run-${hostname}-vm";
};
in
{
#
### PACKAGES
#
packages = forAllSystems (system:
let
pkgs = nixpkgsFor.${system};
in
{
inherit (pkgs) my-website;
});
overlays.default =
final: prev:
{
my-website = final.callPackage ./pkgs/website { };
};
#
### HOSTS
#
nixosConfigurations = {
# <hostname> = mkHost "<hostname>";
server1 = mkHost "server1";
server2 = mkHost "server2";
};
apps = forAllSystems (system: {
# <hostname> = mkVm "<hostname>";
server1 = mkVm "server1";
server2 = mkVm "server2";
});
#
### CHECKS
#
checks = forAllSystems (system:
let
pkgs = nixpkgsFor.${system};
in
{
# <test-name> = pkgs.testers.runNixOSTest (import ./tests/<test-script>.nix { inherit inputs outputs systemMeta; });
test-website = pkgs.nixosTest (import ./tests/test-website.nix { inherit inputs outputs systemMeta; });
test-secrets = pkgs.nixosTest (import ./tests/test-secrets.nix { inherit inputs outputs systemMeta; });
});
#
### SHELLS
#
devShells = forAllSystems (system:
let
pkgs = nixpkgsFor.${system};
in
{
# Development environment
default = pkgs.mkShell {
buildInputs = [
inputs.agenix.packages.${system}.agenix # secrets management
];
shellHook = ''
function dev-help {
echo -e "\nWelcome to a NixOS server development environment !"
echo
echo "Show this flake content:"
echo
echo " nix flake show"
echo
echo "Run server in VM:"
echo
echo " nix run .#<hostname>"
echo
echo "Run tests:"
echo
echo " nix flake check"
echo
echo "Launch interactive test environment:"
echo
echo " nix run .#checks.<system>.<test>.driverInteractive -- --interactive"
echo " start_all()"
echo
echo "Run 'dev-help' to see this message again."
}
dev-help
'';
};
});
};
}