sanic/flake.nix

166 lines
5.2 KiB
Nix
Raw Normal View History

2023-10-17 23:10:58 +02:00
{
2024-08-11 22:19:39 +02:00
description = "sanic - chaos music control";
2023-10-17 23:10:58 +02:00
inputs = {
2024-08-11 22:33:30 +02:00
nixpkgs.url = github:NixOS/nixpkgs/nixpkgs-unstable;
2024-08-11 22:19:39 +02:00
flake-utils.url = github:numtide/flake-utils;
2023-10-24 15:59:04 +02:00
gomod2nix = {
2024-08-11 22:19:39 +02:00
url = github:tweag/gomod2nix;
2023-10-24 15:59:04 +02:00
inputs.nixpkgs.follows = "nixpkgs";
inputs.flake-utils.follows = "flake-utils";
};
2023-10-17 23:10:58 +02:00
};
2023-10-24 15:59:04 +02:00
outputs = { self, nixpkgs, flake-utils, gomod2nix }: flake-utils.lib.eachDefaultSystem (system:
2023-10-17 23:10:58 +02:00
let
2023-10-24 15:59:04 +02:00
pkgs = import nixpkgs {
inherit system;
overlays = [ gomod2nix.overlays.default ];
};
sanic = pkgs.buildGoApplication {
pname = "sanic";
version = "0.0.1";
src = ./.;
modules = ./gomod2nix.toml;
};
2024-01-26 14:18:46 +01:00
in
{
formatter = pkgs.nixpkgs-fmt;
2023-10-17 23:10:58 +02:00
devShells.default = pkgs.mkShell {
2023-10-24 15:59:04 +02:00
buildInputs = with pkgs; [
2023-10-19 17:58:36 +02:00
go
2024-01-26 14:18:46 +01:00
go-tools # staticcheck
2023-10-24 15:59:04 +02:00
gomod2nix.packages.${system}.default
];
packages = with pkgs; [
2023-10-18 00:40:17 +02:00
mpd
mpc-cli
2023-10-17 23:10:58 +02:00
];
};
2024-08-12 15:54:32 +02:00
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=${cfg.ui.port}
tls=${cfg.ui.tls}
certificate=${cfg.ui.certificate}
key=${cfg.ui.key}
[mpd]
host=${cfg.backend.host}
port=${cfg.backend.port}
'';
2024-08-12 16:02:52 +02:00
execCommand = "${cfg.package}/bin/sanic -c '${configFile}'";
2024-08-12 15:54:32 +02:00
in
{
options.services.sanic = {
enable = lib.mkEnableOption "Enables the sanic systemd service.";
2024-08-12 16:02:52 +02:00
package = lib.mkOption {
description = "Package to use.";
type = lib.types.package;
default = sanic;
};
2024-08-12 15:54:32 +02:00
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;
#};
};
2023-10-17 23:10:58 +02:00
}
);
}