forked from unmojang/drasl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.nix
More file actions
202 lines (180 loc) · 5.87 KB
/
flake.nix
File metadata and controls
202 lines (180 loc) · 5.87 KB
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
{
description = "Self-hosted API server for Minecraft";
inputs = {
nixpkgs.url = "nixpkgs/nixos-unstable";
};
outputs =
{
self,
nixpkgs,
}:
let
version =
let
buildConfig = builtins.readFile ./build_config.go;
lines = nixpkgs.lib.strings.splitString "\n" buildConfig;
versionExpr = "^const VERSION = \"([^\"]+)\"$";
findVersion =
lines:
if builtins.length lines == 0 then
builtins.error "VERSION not found in build_config.go"
else
let
match = builtins.match versionExpr (nixpkgs.lib.head lines);
in
if match == null then findVersion (nixpkgs.lib.tail lines) else builtins.elemAt match 0;
in
findVersion lines;
supportedSystems = [
"x86_64-linux"
"x86_64-darwin"
"aarch64-linux"
"aarch64-darwin"
];
# Helper function to generate an attrset '{ x86_64-linux = f "x86_64-linux"; ... }'.
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
overlays = [ ];
nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system overlays; });
nixpkgsCross = forAllSystems (
localSystem:
forAllSystems (crossSystem: import nixpkgs { inherit localSystem crossSystem overlays; })
);
in
{
packages = forAllSystems (
system:
let
buildDrasl =
pkgs:
let
nodejs = pkgs.nodejs_20;
npmDeps = pkgs.importNpmLock.buildNodeModules {
inherit nodejs;
npmRoot = ./.;
};
in
pkgs.buildGoModule {
pname = "drasl";
inherit version;
src = ./.;
nativeBuildInputs = with pkgs; [
nodejs
go-swag
];
# Update whenever Go dependencies change
vendorHash = "sha256-4Rk59bnDFYpraoGvkBUW6Z5fiXUmm2RLwS1wxScWAMQ=";
outputs = [ "out" ];
preConfigure = ''
substituteInPlace build_config.go --replace-fail "\"/usr/share/drasl\"" "\"$out/share/drasl\""
'';
preBuild = ''
ln -s ${npmDeps}/node_modules .
make -o npm-install prebuild
'';
postInstall = ''
mkdir -p "$out/share/drasl"
cp -R ./{assets,view,public,locales} "$out/share/drasl"
'';
};
buildOCIImage =
pkgs:
pkgs.dockerTools.buildLayeredImage {
name = "unmojang/drasl";
contents = with pkgs; [ cacert ];
config.Cmd = [ "${buildDrasl pkgs}/bin/drasl" ];
};
in
rec {
drasl = buildDrasl nixpkgsFor.${system};
drasl-cross-x86_64-linux = buildDrasl nixpkgsCross.${system}.x86_64-linux;
# drasl-cross-x86_64-darwin = buildDrasl nixpkgsCross.${system}.x86_64-darwin;
drasl-cross-aarch64-linux = buildDrasl nixpkgsCross.${system}.aarch64-linux;
# drasl-cross-aarch64-darwin = buildDrasl nixpkgsCross.${system}.aarch64-darwin;
oci = buildOCIImage nixpkgsFor.${system};
oci-cross-x86_64-linux = buildOCIImage nixpkgsCross.${system}.x86_64-linux;
# oci-cross-x86_64-darwin = buildOCIImage nixpkgsCross.${system}.x86_64-darwin;
oci-cross-aarch64-linux = buildOCIImage nixpkgsCross.${system}.aarch64-linux;
# oci-cross-aarch64-darwin = buildOCIImage nixpkgsCross.${system}.aarch64-darwin;
}
);
nixosModules.drasl =
{
config,
lib,
pkgs,
...
}:
with lib;
let
cfg = config.services.drasl;
format = pkgs.formats.toml { };
in
{
options.services.drasl = {
enable = mkEnableOption (lib.mdDoc ''drasl'');
package = mkPackageOption { drasl = self.defaultPackage.${pkgs.stdenv.hostPlatform.system}; } "drasl" { };
settings = mkOption {
type = format.type;
default = { };
description = lib.mdDoc ''
config.toml for drasl
'';
};
};
config = mkIf cfg.enable {
systemd.services.drasl = {
description = "drasl";
wantedBy = [ "multi-user.target" ];
after = [
"network-online.target"
"nss-lookup.target"
];
wants = [
"network-online.target"
"nss-lookup.target"
];
serviceConfig =
let
config = format.generate "config.toml" cfg.settings;
in
{
ExecStart = "${cfg.package}/bin/drasl -config ${config}";
DynamicUser = true;
StateDirectory = "drasl";
Restart = "always";
};
};
};
};
devShells = forAllSystems (
system:
let
pkgs = nixpkgsFor.${system};
in
{
default = pkgs.mkShell {
# https://github.com/go-delve/delve/issues/3085
hardeningDisable = [ "fortify" ];
buildInputs = with pkgs; [
cabal-install
nixfmt-rfc-style
delve
go
go-swag
go-tools
golangci-lint
gopls
gore
gotools
nodejs
pre-commit
sqlite-interactive
swagger-codegen
gettext
];
};
}
);
defaultPackage = forAllSystems (system: self.packages.${system}.drasl);
};
}