-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.nix
More file actions
173 lines (154 loc) · 5.52 KB
/
Copy pathflake.nix
File metadata and controls
173 lines (154 loc) · 5.52 KB
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
166
167
168
169
170
171
172
173
{
description = "Scriptorum - Supernote sync system";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
android-nixpkgs = {
url = "github:tadfisher/android-nixpkgs";
inputs.nixpkgs.follows = "nixpkgs";
};
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { self, nixpkgs, flake-utils, android-nixpkgs, rust-overlay }:
let
# NixOS module (system-independent)
nixosModule = { config, lib, pkgs, ... }:
let
cfg = config.services.scriptorum;
inherit (lib) mkEnableOption mkOption types mkIf;
in
{
options.services.scriptorum = {
enable = mkEnableOption "Scriptorum server";
storageDir = mkOption {
default = "/var/lib/scriptorum/notes";
type = types.str;
description = "Directory where synced note files are stored.";
};
bindAddress = mkOption {
default = "127.0.0.1:3742";
type = types.str;
description = "Address and port for the HTTP server to listen on.";
};
openFirewall = mkOption {
default = false;
type = types.bool;
description = "Open the bind port in the firewall. Only useful when binding to a public interface.";
};
user = mkOption {
default = "scriptorum";
type = types.str;
description = "User to run scriptorum-server as.";
};
group = mkOption {
default = "scriptorum";
type = types.str;
description = "Group to run scriptorum-server as.";
};
archiveDir = mkOption {
default = "/var/lib/scriptorum/notes-archive";
type = types.str;
description = "Directory where archived and conflicted files are stored.";
};
};
config = mkIf cfg.enable {
users.users.${cfg.user} = {
isSystemUser = true;
group = cfg.group;
description = "Scriptorum server";
};
users.groups.${cfg.group} = { };
systemd.tmpfiles.rules = [
"d '${cfg.storageDir}' 0750 ${cfg.user} ${cfg.group} - -"
"d '${cfg.archiveDir}' 0750 ${cfg.user} ${cfg.group} - -"
];
systemd.services.scriptorum = {
description = "Scriptorum server";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
serviceConfig = {
ExecStart = "${self.packages.${pkgs.system}.scriptorum-server}/bin/scriptorum-server --bind ${cfg.bindAddress} --storage ${cfg.storageDir} --archive-dir ${cfg.archiveDir}";
User = cfg.user;
Group = cfg.group;
Restart = "on-failure";
RestartSec = "5s";
# Hardening
NoNewPrivileges = true;
ProtectSystem = "strict";
ProtectHome = true;
ReadWritePaths = [ cfg.storageDir cfg.archiveDir ];
PrivateTmp = true;
};
};
networking.firewall = mkIf cfg.openFirewall {
allowedTCPPorts =
let
port = lib.toInt (lib.last (lib.splitString ":" cfg.bindAddress));
in
[ port ];
};
};
};
in
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
config.allowUnfree = true;
overlays = [ rust-overlay.overlays.default ];
};
androidSdk = android-nixpkgs.sdk.${system} (sdkPkgs: with sdkPkgs; [
cmdline-tools-latest
build-tools-30-0-3
build-tools-34-0-0
platform-tools
platforms-android-30
platforms-android-34
ndk-26-1-10909125
emulator
system-images-android-30-google-apis-x86-64
]);
rustToolchain = pkgs.rust-bin.stable.latest.default.override {
extensions = [ "rust-src" "rust-analyzer" ];
targets = [ "aarch64-linux-android" ];
};
in
{
packages.scriptorum-server = pkgs.rustPlatform.buildRustPackage {
pname = "scriptorum-server";
version = "0.1.0";
src = ./.;
cargoLock.lockFile = ./Cargo.lock;
cargoBuildFlags = [ "-p" "scriptorum-server" ];
};
packages.default = self.packages.${system}.scriptorum-server;
devShells.default = pkgs.mkShell {
buildInputs = with pkgs; [
rustToolchain
cargo-ndk
androidSdk
jdk17
just
ripgrep
caddy
openssl
];
ANDROID_HOME = "${androidSdk}/share/android-sdk";
ANDROID_SDK_ROOT = "${androidSdk}/share/android-sdk";
ANDROID_NDK_ROOT = "${androidSdk}/share/android-sdk/ndk/26.1.10909125";
JAVA_HOME = "${pkgs.jdk17}";
RUST_SRC_PATH = "${rustToolchain}/lib/rustlib/src/rust/library";
shellHook = ''
echo "Scriptorum dev environment loaded"
echo " Rust: $(rustc --version)"
echo " Android SDK: $ANDROID_HOME"
'';
};
}
) // {
nixosModules.default = nixosModule;
};
}