File tree Expand file tree Collapse file tree 2 files changed +37
-1
lines changed
Expand file tree Collapse file tree 2 files changed +37
-1
lines changed Original file line number Diff line number Diff line change 9494 runtimeInputs = [
9595 pkgs . tinyproxy
9696 pkgs . simple-http-server
97+ self' . packages . run-migrations
9798 ] ;
9899 text =
99100 let
109110 } ;
110111 in
111112 ''
113+ database_file=$(mktemp database.XXXX)
114+ run-migrations server/migrations "$database_file"
115+
112116 simple-http-server --index --port 8001 frontend &
113117 PID_FRONTEND=$!
114- cargo run -- --listen-address 0.0.0.0:8002 &
118+ cargo run -- --listen-address 0.0.0.0:8002 --database-url "sqlite://$database_file" &
115119 PID_BACKEND=$!
116120 tinyproxy -d -c ${ proxyConfig } &
117121 PID_PROXY=$!
125129 trap cleanup SIGINT
126130
127131 wait $PID_FRONTEND $PID_BACKEND $PID_PROXY
132+ rm -rf "$database_file"
128133 '' ;
129134 } ;
130135
136+ run-migrations = pkgs . writeShellApplication {
137+ name = "run-migrations" ;
138+ runtimeInputs = [ pkgs . sqlite ] ;
139+ text = ''
140+ >&2 echo "Applying migrations"
141+ for migration_file in "$1"/*.sql; do
142+ >&2 echo "Applying migration: $migration_file"
143+ sqlite3 "$2" < "$migration_file"
144+ done
145+ '' ;
146+ } ;
147+
131148 server-deps = craneLib . buildDepsOnly commonAttrs ;
132149
133150 server-docs = craneLib . cargoDoc (
142159 // {
143160 cargoArtifacts = self' . packages . server-deps ;
144161 meta . mainProgram = "server" ;
162+ passthru = {
163+ migrations = ./server/migrations ;
164+ inherit ( self' . packages ) run-migrations ;
165+ } ;
145166 }
146167 ) ;
147168
Original file line number Diff line number Diff line change 99 optionals
1010 ;
1111 cfg = config . services . server ;
12+ stateDirectory = "/var/lib/server" ;
13+ databasePath = "/var/lib/server/database.db" ;
1214in
1315{
1416 options . services . server = {
3739 '' ;
3840 } ;
3941
42+ database_url = mkOption {
43+ type = types . str ;
44+ default = "sqlite://${ databasePath } " ;
45+ example = "sqlite://${ databasePath } " ;
46+ description = ''
47+ SQlite database to connect to.
48+ '' ;
49+ } ;
50+
4051 metrics = {
4152 enable = lib . mkEnableOption "Prometheus metrics server" ;
4253
7586 [
7687 "--listen-address"
7788 "${ cfg . address } :${ toString cfg . port } "
89+ "--database-url"
90+ "sqlite://${ databasePath } "
7891 ]
7992 ++ optionals cfg . metrics . enable [
8093 "--metrics-listen-address"
92105 RUST_LOG = cfg . logLevel ;
93106 } ;
94107 serviceConfig = {
108+ ExecStartPre = "${ lib . getExe cfg . package . passthru . run-migrations } ${ cfg . package . passthru . migrations } ${ databasePath } " ;
95109 ExecStart = "${ lib . getExe cfg . package } ${ args } " ;
96110 Restart = "always" ;
97111 DynamicUser = true ;
112+ StateDirectory = baseNameOf stateDirectory ;
98113 } ;
99114 } ;
100115 } ;
You can’t perform that action at this time.
0 commit comments