forked from nix-community/authentik-nix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake.nix
216 lines (213 loc) · 8.12 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
{
description = "Nix package, NixOS module and VM integration test for authentik";
inputs = {
flake-utils.url = "github:numtide/flake-utils";
flake-parts.url = "github:hercules-ci/flake-parts";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.05";
poetry2nix = {
url = "github:nix-community/poetry2nix";
inputs = {
nixpkgs.follows = "nixpkgs";
flake-utils.follows = "flake-utils";
};
};
napalm = {
url = "github:nix-community/napalm";
inputs = {
nixpkgs.follows = "nixpkgs";
flake-utils.follows = "flake-utils";
};
};
authentik-src = { # change version string in outputs as well when updating
url = "github:goauthentik/authentik/version/2023.6.1";
flake = false;
};
};
outputs = inputs@{
self,
nixpkgs,
flake-parts,
poetry2nix,
napalm,
authentik-src,
...
}:
flake-parts.lib.mkFlake
{ inherit inputs; }
({ inputs, lib, withSystem, ... }:
let
authentik-version = "2023.6.1"; # to pass to the drvs of some components
in rec {
systems = [
"x86_64-linux"
"aarch64-linux" # not tested
];
flake = {
nixosModules.default = { pkgs, ... }: {
imports = [ ./module.nix ];
services.authentik.authentikComponents = withSystem pkgs.stdenv.hostPlatform.system (
{ config, ... }:
{ inherit (config.packages) celery staticWorkdirDeps migrate pythonEnv frontend gopkgs docs; }
);
};
};
perSystem = { inputs', pkgs, system, ... }: let
inherit (inputs'.poetry2nix.legacyPackages)
mkPoetryEnv
defaultPoetryOverrides;
in {
packages = rec {
docs = napalm.legacyPackages.${system}.buildPackage "${authentik-src}/website" {
version = authentik-version; # 0.0.0 specified upstream in package.json
NODE_ENV = "production";
nodejs = pkgs.nodejs_20;
npmCommands = [
"cp -v ${authentik-src}/SECURITY.md ../SECURITY.md"
"cp -vr ${authentik-src}/blueprints ../blueprints"
"npm install --include=dev"
"npm run build-docs-only"
];
installPhase = ''
mv -v ../website $out
'';
};
frontend = napalm.legacyPackages.${system}.buildPackage "${authentik-src}/web" {
version = authentik-version; # 0.0.0 specified upstream in package.json
packageLock = let
# https://github.com/goauthentik/authentik/issues/6180
srcWithFullyResolvedLockfile = pkgs.applyPatches {
name = "authentik-src-with-patched-package-lock";
src = authentik-src;
patches = [
./web-package-lock.json.patch
];
};
in "${srcWithFullyResolvedLockfile}/web/package-lock.json";
NODE_ENV = "production";
nodejs = pkgs.nodejs_20;
preBuild = ''
ln -sv ${docs} ../website
'';
npmCommands = [
"npm install --include=dev --nodedir=${pkgs.nodejs_20}/include/node --loglevel verbose --ignore-scripts"
"npm run build"
];
installPhase = ''
mkdir $out
mv dist $out/dist
cp -r authentik icons $out
'';
};
pythonEnv = mkPoetryEnv {
projectDir = authentik-src;
python = pkgs.python311;
overrides = [ defaultPoetryOverrides ] ++ (import ./poetry2nix-python-overrides.nix pkgs);
};
# server + outposts
gopkgs = pkgs.buildGo120Module {
pname = "authentik-gopgks";
version = authentik-version;
prePatch = ''
sed -i"" -e 's,./web/dist/,${frontend}/dist/,' web/static.go
sed -i"" -e 's,./web/dist/,${frontend}/dist/,' internal/web/static.go
sed -i"" -e 's,./lifecycle/gunicorn.conf.py,${staticWorkdirDeps}/lifecycle/gunicorn.conf.py,' internal/gounicorn/gounicorn.go
'';
src = pkgs.lib.cleanSourceWith {
src = authentik-src;
filter = (path: _:
(builtins.any (x: x) (
(map (infix: pkgs.lib.hasInfix infix path) [
"/cmd"
"/internal"
])
++
(map (suffix: pkgs.lib.hasSuffix suffix path) [
"/web"
"/web/static.go"
"/web/robots.txt"
"/web/security.txt"
"go.mod"
"go.sum"
])
))
);
};
subPackages = [
"cmd/ldap"
"cmd/server"
"cmd/proxy"
"cmd/radius"
];
vendorSha256 = "sha256-HYj5m4yFqqaxUY3YpLePzjdXnQlTIgk9h9glVeuCoLI=";
nativeBuildInputs = [ pkgs.makeWrapper ];
postInstall = ''
wrapProgram $out/bin/server --prefix PATH : ${pythonEnv}/bin
wrapProgram $out/bin/server --prefix PYTHONPATH : ${staticWorkdirDeps}
'';
};
staticWorkdirDeps = pkgs.linkFarm "authentik-static-workdir-deps" [
{ name = "authentik"; path = "${authentik-src}/authentik"; }
{ name = "locale"; path = "${authentik-src}/locale"; }
{ name = "blueprints"; path = "${authentik-src}/blueprints"; }
{ name = "internal"; path = "${authentik-src}/internal"; }
{ name = "lifecycle"; path = "${authentik-src}/lifecycle"; }
{ name = "schemas"; path = "${authentik-src}/schemas"; }
{ name = "web"; path = frontend; }
];
migrate = pkgs.runCommandLocal "authentik-migrate.py" {
nativeBuildInputs = [ pkgs.makeWrapper ];
} ''
mkdir -vp $out/bin
cp ${authentik-src}/lifecycle/migrate.py $out/bin/migrate.py
chmod +w $out/bin/migrate.py
patchShebangs $out/bin/migrate.py
substituteInPlace $out/bin/migrate.py \
--replace \
'migration in Path(__file__).parent.absolute().glob("system_migrations/*.py")' \
'migration in Path("${staticWorkdirDeps}/lifecycle").glob("system_migrations/*.py")'
wrapProgram $out/bin/migrate.py \
--prefix PATH : ${pythonEnv}/bin \
--prefix PYTHONPATH : ${staticWorkdirDeps}
'';
# worker
celery = pkgs.runCommandLocal "authentik-celery" {
nativeBuildInputs = [ pkgs.makeWrapper ];
} ''
mkdir -vp $out/bin
ln -sv ${pythonEnv}/bin/celery $out/bin/celery
wrapProgram $out/bin/celery \
--prefix PYTHONPATH : ${staticWorkdirDeps}
'';
# terraform provider
terraform-provider-authentik = pkgs.buildGo118Module rec {
pname = "terraform-provider-authentik";
version = "2023.5.0";
src = pkgs.fetchFromGitHub {
owner = "goauthentik";
repo = pname;
rev = "v${version}";
sha256 = "sha256-fPdO8GpP24VDHEHxlIEaJE6H+i/HyIiD1cogRXVDN2k=";
};
doCheck = false; # tests are run against authentik -> vm test
vendorSha256 = "sha256-tVWb50HuHiLdOWT5nHCA5CKzqVXOyX4SFvO0Hb8do6k=";
postInstall = ''
path="$out/libexec/terraform-providers/registry.terraform.io/goauthentik/authentik/${version}/''${GOOS}_''${GOARCH}/"
mkdir -p "$path"
mv $out/bin/${pname} $path/${pname}_v${version}
'';
};
};
checks.default = (import ./test.nix {
inherit pkgs;
inherit (self) nixosModules;
});
devShells.default = pkgs.mkShell {
packages = [
# to generate a v2 lockfile from the v3 lockfile provided by upstream:
# npm install --lockfile-version 2 --package-lock-only
pkgs.nodejs
];
};
};
});
}