-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake.nix
174 lines (157 loc) · 6.72 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
{
description = ''
A garnix module for projects using Rust.
Add build and runtime dependencies and optionally deploy a web server.
[Documentation](https://garnix.io/docs/modules/rust) - [Source](https://github.com/garnix-io/rust-module).
'';
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11";
inputs.crane.url = "github:ipetkov/crane";
outputs = { self, nixpkgs, crane }:
let
lib = nixpkgs.lib;
webServerSubmodule.options = {
command = lib.mkOption
{
type = lib.types.nonEmptyStr;
description = "The command to run to start the server in production.";
example = "server --port 8000";
} // { name = "server command"; };
port = lib.mkOption {
type = lib.types.port;
description = "Port to forward incoming HTTP requests to. The server command has to listen on this port.";
example = 8000;
default = 8000;
};
path = lib.mkOption
{
type = lib.types.nonEmptyStr;
description = "Path your Rust server will be hosted on.";
default = "/";
} // { name = "API path"; };
};
rustSubmodule.options = {
src = lib.mkOption
{
type = lib.types.path;
description = "A path to the directory containing `Cargo.lock`, `Cargo.toml`, and `src`.";
example = "./.";
} // { name = "source directory"; };
webServer = lib.mkOption {
type = lib.types.nullOr (lib.types.submodule webServerSubmodule);
description = "Whether to create an HTTP server based on this Rust project.";
default = null;
};
devTools = lib.mkOption
{
type = lib.types.listOf lib.types.package;
description = "A list of packages make available in the devshell for this project (and `default` devshell). This is useful for things like LSPs, formatters, etc.";
default = [ ];
} // { name = "development tools"; };
buildDependencies = lib.mkOption {
type = lib.types.listOf lib.types.package;
description = "A list of dependencies required to build this package. They are made available in the devshell, and at build time.";
default = [ ];
};
runtimeDependencies = lib.mkOption {
type = lib.types.listOf lib.types.package;
description = "A list of dependencies required at runtime. They are made available in the devshell, at build time, and are available on the server at runtime.";
default = [ ];
};
};
in
{
garnixModules.default = { pkgs, config, ... }:
let
craneLib = crane.mkLib pkgs;
craneArgsByProject = builtins.mapAttrs
(name: projectConfig: rec {
src = craneLib.cleanCargoSource projectConfig.src;
cargoArtifacts = craneLib.buildDepsOnly { inherit src buildInputs; };
buildInputs = projectConfig.buildDependencies ++ projectConfig.runtimeDependencies;
})
config.rust;
in
{
options = {
rust = lib.mkOption {
type = lib.types.attrsOf (lib.types.submodule rustSubmodule);
description = "An attrset of Rust projects to generate.";
};
};
config = {
packages = builtins.mapAttrs
(name: projectConfig:
craneLib.buildPackage {
inherit (craneArgsByProject.${name}) src cargoArtifacts buildInputs;
}
)
config.rust;
checks = lib.foldlAttrs
(acc: name: projectConfig: acc // {
"${name}-cargo-clippy" = craneLib.cargoClippy {
inherit (craneArgsByProject.${name}) src cargoArtifacts buildInputs;
cargoClippyExtraArgs = "--all-targets -- --deny warnings";
};
"${name}-cargo-fmt" = craneLib.cargoFmt { inherit (craneArgsByProject.${name}) src; };
"${name}-cargo-doc" = craneLib.cargoDoc { inherit (craneArgsByProject.${name}) src cargoArtifacts buildInputs; };
})
{ }
config.rust;
devShells = builtins.mapAttrs
(name: projectConfig:
craneLib.devShell {
packages =
projectConfig.devTools ++
projectConfig.buildDependencies ++
projectConfig.runtimeDependencies;
}
)
config.rust;
nixosConfigurations =
let
hasAnyWebServer =
builtins.any (projectConfig: projectConfig.webServer != null)
(builtins.attrValues config.rust);
in
lib.mkIf hasAnyWebServer {
default =
# Global NixOS configuration
[{
services.nginx = {
enable = true;
recommendedProxySettings = true;
recommendedOptimisation = true;
virtualHosts.default = {
default = true;
};
};
networking.firewall.allowedTCPPorts = [ 80 ];
}]
++
# Per project NixOS configuration
(builtins.attrValues (builtins.mapAttrs
(name: projectConfig: lib.mkIf (projectConfig.webServer != null) {
environment.systemPackages = projectConfig.runtimeDependencies;
systemd.services.${name} = {
description = "${name} Rust garnix module";
wantedBy = [ "multi-user.target" ];
after = [ "network-online.target" ];
wants = [ "network-online.target" ];
serviceConfig = {
Type = "simple";
DynamicUser = true;
ExecStart = lib.getExe (pkgs.writeShellApplication {
name = "start-${name}";
runtimeInputs = [ config.packages.${name} ] ++ projectConfig.runtimeDependencies;
text = projectConfig.webServer.command;
});
};
};
services.nginx.virtualHosts.default.locations.${projectConfig.webServer.path}.proxyPass = "http://localhost:${toString projectConfig.webServer.port}";
})
config.rust));
};
};
};
};
}