write nixos-module for declaration of systemd service

This commit is contained in:
2025-11-08 12:34:42 -06:00
parent da0ae1646b
commit 9adb8c9274
+65 -2
View File
@@ -10,8 +10,11 @@
pkgs = nixpkgs.legacyPackages.${system}; pkgs = nixpkgs.legacyPackages.${system};
reactApp = pkgs.buildNpmPackage { reactApp = pkgs.buildNpmPackage {
name = "react frontend"; name = "React Frontend";
version = "0.1.0"; version = "0.1.0";
src = "./.";
npmDepsHash = "";
# TODO: Configure npm building of frontend # TODO: Configure npm building of frontend
@@ -20,7 +23,67 @@
nixosModule = {config, lib, pkgs, ...}: nixosModule = {config, lib, pkgs, ...}:
let opts = config.services.blog-service; in let opts = config.services.blog-service; in
{ {
# TODO: Write out options for frontend service (port hosting, nginx, backend ports?) enableFrontend = lib.mkEnableOption "Enable Frontend of blog";
port = lib.mkOption {
type = lib.types.port;
description = "Port to host frontend";
};
credentialsFile = lib.mkOption {
type = lib.types.path;
description = "File containing secret environment variables";
};
default-nginx = {
enableNginx = lib.mkEnableOption "Enable nginx reverse proxy";
domainName = lib.mkOption {
type = lib.types.string;
description = "Domain name to host service";
};
};
config = lib.mkIf opts.enableFrontend {
systemd.services.blog-service = {
description = "Frontend of self-hosted blog";
wantedby = ["multi-user.target"];
after = ["network.target"];
environment = {
PORT = toString opts.port;
};
serviceConfig = {
ExecStart = "${reactApp}/bin/blog-service";
Restart = "always";
Type = "simple";
DynamicUser = "yes";
WorkingDirectory = "${reactApp}";
EnvironmentFile = "${opts.credentialsFile}";
};
};
services.nginx = lib.mkIf opts.default-nginx.enableNginx {
enable = true;
virtualHosts."${opts.default-nginx.domainName}" = {
forceSSL = true;
useACMEHost = let
b = builtins;
s = lib.strings;
fl = s.splitString "." "${opts.default-nginx.domainName}";
in b.concatStringsSep "." [ (b.elemAt fl (b.length fl - 2)) (b.elemAt fl (b.length fl - 1)) ];
locations."/" = {
proxyPass = "http://localhost:${toString opts.port}";
};
};
};
networking.firewall.allowedTCPPorts = lib.mkMerge [
(lib.mkIf opts.enableFrontend [ opts.port ])
(lib.mkIf opts.default-nginx.enableNginx [ 80 443 ])
];
};
}; };
in { in {