-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake.nix
140 lines (125 loc) · 5.34 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
{
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 7000";
} // { name = "server command"; };
port = lib.mkOption {
type = lib.types.port;
description = "Port to forward incoming http requests to";
example = 7000;
};
path = lib.mkOption {
type = lib.types.nonEmptyStr;
description = "Path to host your rust server 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 = [];
} // { name = "build dependencies"; };
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 = [];
} // { name = "runtime dependencies"; };
};
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.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));
};
};
};
}