-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
165 lines (162 loc) · 5.22 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
{
description = "sanic - chaos music control";
inputs = {
nixpkgs.url = github:NixOS/nixpkgs/nixpkgs-unstable;
flake-utils.url = github:numtide/flake-utils;
gomod2nix = {
url = github:tweag/gomod2nix;
inputs.nixpkgs.follows = "nixpkgs";
inputs.flake-utils.follows = "flake-utils";
};
};
outputs = { self, nixpkgs, flake-utils, gomod2nix }: flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ gomod2nix.overlays.default ];
};
sanic = pkgs.buildGoApplication {
pname = "sanic";
version = "0.0.1";
src = ./.;
modules = ./gomod2nix.toml;
};
in
{
formatter = pkgs.nixpkgs-fmt;
devShells.default = pkgs.mkShell {
buildInputs = with pkgs; [
go
go-tools # staticcheck
gomod2nix.packages.${system}.default
];
packages = with pkgs; [
mpd
mpc-cli
];
};
packages.default = sanic;
nixosModules.default = { config, lib, pkgs, options, ... }:
let
cfg = config.services.sanic;
configFile = pkgs.writeText "config.ini" ''
[ui]
host=${cfg.ui.host}
port=${toString cfg.ui.port}
tls=${if cfg.ui.tls then "true" else "false"}
certificate=${toString cfg.ui.certificate}
key=${toString cfg.ui.key}
[mpd]
host=${cfg.backend.host}
port=${toString cfg.backend.port}
'';
execCommand = "${cfg.package}/bin/sanic -c '${configFile}'";
in
{
options.services.sanic = {
enable = lib.mkEnableOption "Enables the sanic systemd service.";
package = lib.mkOption {
description = "Package to use.";
type = lib.types.package;
default = sanic;
};
ui = lib.mkOption {
description = "Setting for HTTP(S) UI.";
example = lib.literalExpression ''
{
host = "[::1]";
port = 443;
tls = true;
certificate = "${config.security.acme.certs."sanic.example.com".directory}/fullchain.pem";
key = "${config.security.acme.certs."sanic.example.com".directory}/key.pem";
}
'';
default = {
host = "[::1]";
port = 80;
tls = false;
};
type = lib.types.submodule {
options = {
host = lib.mkOption {
type = lib.types.str;
default = "[::1]";
description = "Host to bind to.";
};
port = lib.mkOption {
type = lib.types.port;
default = 80;
description = "Port to listen on.";
};
tls = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Enables HTTPS.";
};
certificate = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = "Path to TLS certificate for HTTPS.";
};
key = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = "Path to TLS key for HTTPS.";
};
};
};
};
backend = lib.mkOption {
description = "Configure MPD backend.";
example = lib.literalExpression ''
{
host = "localhost";
port = 6600;
}
'';
default = {
host = "localhost";
port = 6600;
};
type = lib.types.submodule {
options = {
host = lib.mkOption {
type = lib.types.str;
default = "localhost";
description = "Hostname or IP of MPD instance.";
};
port = lib.mkOption {
type = lib.types.port;
default = 6600;
description = "Port of MPD instance.";
};
};
};
};
};
config = lib.mkIf cfg.enable {
systemd.services."sanic" = {
description = "sanic - chaos music control";
wants = [ "network-online.target" ];
after = [ "network-online.target" ];
serviceConfig = {
Restart = "always";
RestartSec = 30;
ExecStart = execCommand;
User = "sanic";
Group = "sanic";
AmbientCapabilities = lib.mkIf (cfg.ui.port < 1000) [ "CAP_NET_BIND_SERVICE" ];
CapabilityBoundingSet = lib.mkIf (cfg.ui.port < 1000) [ "CAP_NET_BIND_SERVICE" ];
NoNewPrivileges = true;
};
wantedBy = [ "multi-user.target" ];
};
};
#meta = {
# maintainers = with lib.maintainers; [ xengi ];
# doc = ./default.xml;
#};
};
}
);
}