-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake.nix
112 lines (100 loc) · 2.92 KB
/
flake.nix
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
{
inputs = {
flake-utils.url = "github:numtide/flake-utils";
naersk.url = "github:nix-community/naersk";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
};
outputs = { self, flake-utils, naersk, nixpkgs }:
let
system = "x86_64-linux";
pkgs = (import nixpkgs) {
inherit system;
};
# Credit to Ivan Petov for getting sqlx macros working with nix
# https://ipetkov.dev/blog/building-with-sqlx-on-nix/
naersk' = pkgs.callPackage naersk { };
migrations = ./migrations;
sqlx-db = pkgs.runCommand "sqlx-db-prepare"
{
nativeBuildInputs = [ pkgs.sqlx-cli ];
} ''
mkdir $out
export DATABASE_URL=sqlite:$out/db.sqlite3
sqlx database create
sqlx migrate --source ${migrations} run
'';
in
rec {
fred = naersk'.buildPackage {
src = ./.;
nativeBuildInputs = with pkgs; [
sqlite
];
# Haha offline mode more like, my sanity is gone
overrideMain = old: {
linkDb = ''
export DATABASE_URL=sqlite:${sqlx-db}/db.sqlite3
'';
preBuildPhases = [ "linkDb" ] ++ (old.preBuildPhases or [ ]);
};
};
nixosModule =
{ config, options, lib, pkgs, ... }:
let
cfg = config.services.fred;
in
with lib; {
options = {
services.fred = {
enable = mkOption {
default = false;
type = with types; bool;
description = "Start the fred service for a user";
};
envFilePath = mkOption {
type = with types; uniq str;
description = "Path the .env file";
};
dbFilePath = mkOption {
type = with types; uniq str;
description = "Path the .env file";
};
};
};
config = mkIf cfg.enable
{
environment.systemPackages = [ fred ];
systemd.services.fred = {
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
environment = {
DATABASE_URL = "sqlite://${cfg.dbFilePath}";
};
serviceConfig = {
Type = "simple";
EnvironmentFile = "${cfg.envFilePath}";
ExecStart = ''
${fred}/bin/fred
'';
};
};
};
};
# For `nix develop` (optional, can be skipped):
devShells.${system}.default = pkgs.mkShell {
shellHook = ''
export $(cat .env)
'';
nativeBuildInputs = with pkgs; [
rustc
cargo
clippy
rust-analyzer
rustfmt
sqlx-cli
sqliteman
sqlite
];
};
};
}