-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
153 lines (131 loc) · 4.67 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
{
description = "NixOS configuration for Pokétwo";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
systems.url = "github:nix-systems/default";
agenix.url = "github:ryantm/agenix";
transpire.url = "github:oliver-ni/transpire";
# transpire.url = "path:/Users/oliver/Development/github.com/oliver-ni/transpire-nix";
agenix.inputs.nixpkgs.follows = "nixpkgs";
transpire.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = { nixpkgs, systems, agenix, transpire, ... }:
let
# ========================
# NixOS Host Configuration
# ========================
# Put modules common to all hosts here.
commonModules = [
agenix.nixosModules.default
./modules/poketwo/auth.nix
./modules/poketwo/cloudflare-warp.nix
./modules/poketwo/kubernetes.nix
./modules/poketwo/locale.nix
./modules/poketwo/nat64.nix
./modules/poketwo/network.nix
./modules/poketwo/shell.nix
./modules/poketwo/tailscale.nix
./profiles/base.nix
];
hosts = nixpkgs.lib.mapAttrs'
(filename: _: {
name = nixpkgs.lib.nameFromURL filename ".";
value = [ ./hosts/${filename} ];
})
(builtins.readDir ./hosts);
# =======================
# Transpire Configuration
# =======================
fs = nixpkgs.lib.fileset;
allNixFiles = fs.fileFilter (file: file.hasExt "nix") ./.;
kubernetesExtraModules = fs.toList
(fs.intersection allNixFiles ./kubernetes/extras);
kubernetesModules = fs.toList
(fs.intersection
(fs.fileFilter (file: file.hasExt "nix") ./.)
(fs.difference ./kubernetes ./kubernetes/extras));
openApiSpec = ./kube-openapi.json;
# =====================
# nixpkgs Configuration
# =====================
overlays = [
agenix.overlays.default
];
pkgsFor = system: import nixpkgs {
inherit overlays system;
config = { allowUnfree = true; };
};
# =====================
# Colmena Configuration
# =====================
colmenaHosts = builtins.mapAttrs
(host: modules: {
imports = commonModules ++ modules;
deployment.buildOnTarget = true;
deployment.targetUser = "oliver";
deployment.allowLocalDeployment = true;
})
hosts;
forAllSystems = fn: nixpkgs.lib.genAttrs
(import systems)
(system: fn system (pkgsFor system));
in
{
colmena = colmenaHosts // {
meta = { nixpkgs = pkgsFor "x86_64-linux"; };
};
devShells = forAllSystems (system: pkgs: {
default = pkgs.mkShell {
packages = [ pkgs.colmena pkgs.agenix ];
};
});
packages = forAllSystems (system: pkgs: {
kubernetes = transpire.lib.${system}.build.cluster {
inherit openApiSpec;
modules = kubernetesModules ++ kubernetesExtraModules;
};
docs = transpire.lib.${system}.buildDocs {
inherit openApiSpec;
};
# This is used for the `push-vault-secrets` app
# It's a bit of a hack, but it works for now :)
__raw-secrets-to-push = builtins.groupBy
(obj: obj.metadata.namespace)
(builtins.filter
(obj: obj.apiVersion == "v1" && obj.kind == "Secret")
(transpire.lib.${system}.evalModules {
inherit openApiSpec;
modules = kubernetesModules;
}).config.build.objects);
});
apps = forAllSystems (system: pkgs: {
update-kube-openapi = {
type = "app";
program = toString (pkgs.writers.writeBash "update-kube-openapi" ''
${pkgs.kubectl}/bin/kubectl get --raw /openapi/v2 > kube-openapi.json
'');
};
push-vault-secrets = {
type = "app";
program = toString (pkgs.writers.writeBash "push-vault-secrets" ''
set -e
if [[ $# -eq 0 ]] ; then
echo 'Usage: push-vault-secrets <namespace>'
exit 1
fi
nix eval --json --impure ".#__raw-secrets-to-push.$1" \
| ${pkgs.jq}/bin/jq -r -c '.[] |
.metadata.namespace + "/" + .metadata.name,
(.data // {} | .[] |= @base64d) + (.stringData // {})
' \
| while read -r path; read -r data; do
read -r -p "Push $path? [y/N] " choice <&2
if [[ $choice =~ ^[Yy] ]] ; then
${pkgs.vault-bin}/bin/vault kv put -mount=hfym-ds "$path" - <<< "$data" > /dev/null
fi
done
'');
};
});
};
}