Skip to content

Commit 2b6112f

Browse files
author
Damian Poddebniak
committed
build: Add Nix/NixOS support
1 parent a4d9f39 commit 2b6112f

6 files changed

Lines changed: 256 additions & 0 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,7 @@ web-src/geckodriver.log
8080
venv/
8181
/venv2/
8282
e2e_venv/
83+
84+
# Nix / NixOS
85+
/result
86+
/*.qcow2

flake.lock

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
description = "Script-server";
3+
4+
inputs = {
5+
nixpkgs.url = "github:NixOS/nixpkgs/26.05";
6+
flake-parts.url = "github:hercules-ci/flake-parts";
7+
};
8+
9+
outputs =
10+
inputs@{
11+
self,
12+
nixpkgs,
13+
flake-parts,
14+
...
15+
}:
16+
flake-parts.lib.mkFlake { inherit inputs; } {
17+
systems = [
18+
"x86_64-linux"
19+
"aarch64-linux"
20+
"aarch64-darwin"
21+
];
22+
perSystem =
23+
{ pkgs, ... }:
24+
{
25+
packages = {
26+
script-server = pkgs.python3Packages.callPackage ./nix/package.nix { };
27+
28+
script-server-web = pkgs.callPackage ./nix/package-web.nix { };
29+
30+
demo = self.nixosConfigurations.demo.config.system.build.vm;
31+
};
32+
33+
formatter = pkgs.nixfmt-tree;
34+
};
35+
}
36+
// {
37+
overlays.default = final: prev: {
38+
script-server = final.python3Packages.callPackage ./nix/package.nix { };
39+
40+
script-server-web = final.callPackage ./nix/package-web.nix { };
41+
};
42+
43+
nixosModules.default = import ./nix/module.nix;
44+
45+
nixosConfigurations.demo = nixpkgs.lib.nixosSystem {
46+
modules = [
47+
self.nixosModules.default
48+
{
49+
nixpkgs = {
50+
overlays = [ self.overlays.default ];
51+
hostPlatform = "x86_64-linux";
52+
};
53+
54+
users.users.root.password = "changeit";
55+
56+
services = {
57+
script-server = {
58+
enable = true;
59+
};
60+
};
61+
62+
system.stateVersion = "26.05";
63+
}
64+
];
65+
};
66+
};
67+
}

nix/module.nix

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{
2+
config,
3+
pkgs,
4+
lib,
5+
...
6+
}:
7+
let
8+
cfg = config.services.script-server;
9+
json = pkgs.formats.json { };
10+
in
11+
{
12+
options.services.script-server = {
13+
enable = lib.mkEnableOption "Whether to enable script-server.";
14+
15+
package = lib.mkOption {
16+
type = lib.types.package;
17+
default = pkgs.script-server;
18+
description = "Script-server package to use.";
19+
};
20+
21+
package-web = lib.mkOption {
22+
type = lib.types.package;
23+
default = pkgs.script-server-web;
24+
description = "Script-server-web package to use.";
25+
};
26+
27+
settings = lib.mkOption {
28+
type = json.type;
29+
default = { };
30+
description = "Server configuration, see <https://github.com/bugy/script-server/wiki/Server-configuration>.";
31+
};
32+
33+
configuration = lib.mkOption {
34+
type = lib.types.str;
35+
default = "/var/lib/script-server/conf";
36+
description = "Script configuration, see <https://github.com/bugy/script-server/wiki/Script-config>.";
37+
};
38+
};
39+
40+
config = lib.mkIf cfg.enable {
41+
environment.systemPackages = [ cfg.package ];
42+
43+
environment.etc."script-server/conf.json".source =
44+
json.generate "script-server-config" cfg.settings;
45+
46+
# See <https://github.com/bugy/script-server/wiki/Running-as-a-linux-service#systemd>
47+
systemd.services.script-server = {
48+
enable = true;
49+
50+
description = "Script Server";
51+
after = [ "network.target" ];
52+
wantedBy = [ "multi-user.target" ];
53+
54+
serviceConfig = {
55+
Restart = "always";
56+
RestartSec = "1s";
57+
ExecStart = ''
58+
${cfg.package}/bin/script-server \
59+
--config-file /etc/script-server/conf.json \
60+
--config-dir ${cfg.configuration} \
61+
--log-folder /var/log/script-server \
62+
--tmp-folder /tmp
63+
'';
64+
65+
# `main.py` expects `web` to be in working directory.
66+
WorkingDirectory = "${cfg.package-web}";
67+
StateDirectory = "script-server";
68+
LogsDirectory = "script-server";
69+
70+
DynamicUser = true;
71+
PrivateTmp = true;
72+
};
73+
};
74+
};
75+
}

nix/package-web.nix

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{ lib, buildNpmPackage }:
2+
buildNpmPackage {
3+
pname = "script-server-web";
4+
version = "1.18.0";
5+
6+
src = ../web-src;
7+
8+
npmDepsHash = "sha256-aBZDh2NcDU4OpsdTS8JqwGMa8WeIoyW9I6bUwTXfllU=";
9+
10+
makeCacheWritable = true;
11+
12+
# See <https://stackoverflow.com/questions/75959563/node-js-err-ossl-evp-unsupported-error-when-running-npm-run-start>
13+
NODE_OPTIONS = "--openssl-legacy-provider";
14+
15+
installPhase = ''
16+
mkdir "$out"
17+
cp -r ../web "$out"
18+
'';
19+
}

nix/package.nix

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
let
2+
toml = builtins.fromTOML (builtins.readFile ./../pyproject.toml);
3+
in
4+
{
5+
lib,
6+
buildPythonPackage,
7+
tornado,
8+
setuptools,
9+
}:
10+
buildPythonPackage {
11+
pname = toml.project.name;
12+
version = toml.project.version;
13+
pyproject = true;
14+
15+
src = lib.fileset.toSource {
16+
root = ./..;
17+
fileset = lib.fileset.unions [
18+
./../pyproject.toml
19+
./../src
20+
];
21+
};
22+
23+
build-system = [
24+
setuptools
25+
];
26+
27+
dependencies = [
28+
tornado
29+
];
30+
}

0 commit comments

Comments
 (0)