Create nixos service

This commit is contained in:
2023-11-08 23:14:57 -07:00
parent 320b2635fa
commit e06f47d2ff
6 changed files with 128 additions and 15 deletions

View File

@@ -21,6 +21,7 @@ import (
)
var imageDir = "./img"
var port = "8080"
func getRandomFile(dir string) (string, error) {
files, err := ioutil.ReadDir(dir)
@@ -109,7 +110,12 @@ func basicAuth(next http.Handler) http.Handler {
func main() {
if len(os.Args) > 1 {
imageDir = os.Args[1]
port = os.Args[1]
}
fmt.Println("Starting on port: ", port)
if len(os.Args) > 2 {
imageDir = os.Args[2]
}
fmt.Println("Choosing images from: ", imageDir)
@@ -128,6 +134,6 @@ func main() {
fmt.Println("Started server")
// Start the HTTP server on port 8080 and log any errors
log.Fatal(http.ListenAndServe("0.0.0.0:8080", router))
// Start the HTTP server
log.Fatal(http.ListenAndServe("0.0.0.0:"+port, router))
}

23
server/service-test.nix Normal file
View File

@@ -0,0 +1,23 @@
{ nixpkgs, system, service }:
with import (nixpkgs + "/nixos/lib/testing-python.nix") { inherit system; };
simpleTest {
name = "dynamic-frame-server";
nodes.machine = { config, pkgs, ... }: {
imports = [ service ];
virtualisation.memorySize = 256;
services.picture-frame-server = {
enable = true;
imgDir = "/tmp";
};
};
testScript =
''
machine.start()
machine.wait_for_unit("multi-user.target")
machine.wait_for_unit("picture-frame-server")
'';
}

55
server/service.nix Normal file
View File

@@ -0,0 +1,55 @@
{ overlay }:
{ config, pkgs, lib, ... }:
let
cfg = config.services.picture-frame-server;
in
{
options.services.picture-frame-server = {
enable = lib.mkEnableOption "enable picture-frame-server";
user = lib.mkOption {
type = lib.types.str;
default = "picture-frame-server";
description = ''
The user the server should run as
'';
};
group = lib.mkOption {
type = lib.types.str;
default = "picture-frame-server";
description = ''
The group the server should run as
'';
};
imgDir = lib.mkOption {
type = lib.types.str;
description = ''
Directory of images that the server will serve
'';
};
port = lib.mkOption {
type = lib.types.int;
default = 18450;
description = ''
The port the server runs on
'';
};
};
config = lib.mkIf cfg.enable {
nixpkgs.overlays = [ overlay ];
users.users.${cfg.user} = {
isSystemUser = true;
group = cfg.group;
};
users.groups.${cfg.group} = { };
systemd.services.picture-frame-server = {
enable = true;
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
serviceConfig.ExecStart = "${pkgs.picture-frame.server}/bin/server ${toString cfg.port} ${cfg.imgDir}";
serviceConfig.User = cfg.user;
serviceConfig.Group = cfg.group;
};
};
}