Skip to content

Commit 9e45199

Browse files
committed
Get local dev database to run
1 parent 8bd2b07 commit 9e45199

9 files changed

+173
-158
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,9 @@ with Nix(OS).
4343
# Test
4444

4545
```bash
46-
curl --connect-to localhost:80:mycontainer:80 --connect-to localhost:443:mycontainer:443 http://localhost -k -L
46+
sudo nixos-container create db-dev --flake .#db-dev
47+
sudo nixos-container start db-dev --flake .#db-dev
48+
curl --connect-to localhost:80:all:80 --connect-to localhost:443:all:443 http://localhost -k -L
49+
sudo nixos-container update db-dev --flake .#db-dev
50+
sudo nixos-container root-login db-dev --flake .#db-dev
4751
```

flake.nix

+76-68
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,42 @@
2828
lib.genAttrs systems
2929
(system: f system (nixpkgs.legacyPackages.${system}));
3030
in {
31-
packages."x86_64-linux".rustnixos = pkgs.callPackage ./nix/rustnixos.package.nix {};
32-
packages."x86_64-linux".migration-data = pkgs.callPackage ./nix/migration-data.package.nix {};
33-
# packages."x86_64-linux".postgresql-devVM =
34-
# self.nixosConfigurations.postgresql-devVM.config.system.build.vm;
31+
packages."x86_64-linux".rustnixos =
32+
pkgs.callPackage ./nix/rustnixos.package.nix { };
33+
packages."x86_64-linux".migration-data =
34+
pkgs.callPackage ./nix/migration-data.package.nix { };
3535

3636
nixosModules.rustnixos = import ./module.nix;
3737
nixosModules.default = import ./module.nix;
3838
nixosModules.caddy = import ./nix/caddy.module.nix;
39-
nixosModules.postgresql-dev = import ./nixos-modules/postgresql-dev.nix;
39+
nixosModules.db-dev = import ./nix/postgresql-dev.nix;
4040

41+
# Run database+migration only in container for dev
42+
nixosConfigurations.db-dev = nixpkgs.lib.nixosSystem {
43+
inherit system;
44+
specialArgs = attrs // {
45+
inherit (self.packages.${system}) migration-data;
46+
inherit system;
47+
};
48+
49+
modules = [
50+
self.nixosModules.db-dev
51+
({ pkgs, config, ... }: {
52+
boot.isContainer = true;
53+
system.stateVersion = "23.11";
54+
# firewall seem to be enabled by default
55+
networking.firewall.enable = false;
56+
})
57+
];
58+
};
4159

4260
# Run whole setup in container
43-
nixosConfigurations.mycontainer = nixpkgs.lib.nixosSystem {
61+
nixosConfigurations.all = nixpkgs.lib.nixosSystem {
4462
inherit system;
45-
specialArgs = attrs // { inherit (self.packages.${system}) migration-data; inherit system;};
63+
specialArgs = attrs // {
64+
inherit (self.packages.${system}) migration-data;
65+
inherit system;
66+
};
4667
modules = [
4768
self.nixosModules.rustnixos
4869
self.nixosModules.caddy
@@ -53,68 +74,55 @@
5374
})
5475
];
5576
};
56-
# Run database setup in container
57-
nixosConfigurations.postgresql-devVM = nixpkgs.lib.nixosSystem {
58-
inherit system;
59-
specialArgs = attrs // { inherit (self.packages.${system}) migration-data; inherit system;};
60-
61-
modules = [
62-
self.nixosModules.postgresql-dev
63-
({ pkgs, config, ... }: {
64-
# Only allow this to boot as a container
65-
boot.isContainer = true;
66-
# Make VM output to the terminal instead of a separate window
67-
virtualisation.vmVariant.virtualisation.graphics = false;
68-
system.stateVersion = "23.11";
69-
})
70-
];
77+
#-----------------------------------------------------------
78+
# The following line names the configuration as hetzner-cloud
79+
# This name will be referenced when nixos-remote is run
80+
#-----------------------------------------------------------
81+
nixosConfigurations.hetzner-cloud = nixpkgs.lib.nixosSystem {
82+
inherit system;
83+
specialArgs = attrs // {
84+
inherit (self.packages.${system}) migration-data;
85+
inherit system;
86+
};
87+
modules = [
88+
({ modulesPath, ... }: {
89+
imports = [
90+
(modulesPath + "/installer/scan/not-detected.nix")
91+
(modulesPath + "/profiles/qemu-guest.nix")
92+
disko.nixosModules.disko
93+
agenix.nixosModules.default
94+
self.nixosModules.rustnixos
95+
self.nixosModules.caddy
96+
];
97+
disko.devices =
98+
import ./nix/disk-config.disko.nix { lib = nixpkgs.lib; };
99+
age.secrets.secret1.file = ./secrets/secret1.age;
100+
boot.loader.grub = {
101+
devices = [ "/dev/sda" ];
102+
efiSupport = true;
103+
efiInstallAsRemovable = true;
71104
};
72-
73-
#-----------------------------------------------------------
74-
# The following line names the configuration as hetzner-cloud
75-
# This name will be referenced when nixos-remote is run
76-
#-----------------------------------------------------------
77-
nixosConfigurations.hetzner-cloud = nixpkgs.lib.nixosSystem {
78-
inherit system;
79-
specialArgs = attrs // { inherit (self.packages.${system}) migration-data; inherit system;};
80-
modules = [
81-
({modulesPath, ... }: {
82-
imports = [
83-
(modulesPath + "/installer/scan/not-detected.nix")
84-
(modulesPath + "/profiles/qemu-guest.nix")
85-
disko.nixosModules.disko
86-
agenix.nixosModules.default
87-
self.nixosModules.rustnixos
88-
self.nixosModules.caddy
89-
];
90-
disko.devices = import ./nix/disk-config.disko.nix {
91-
lib = nixpkgs.lib;
92-
};
93-
age.secrets.secret1.file = ./secrets/secret1.age;
94-
boot.loader.grub = {
95-
devices = [ "/dev/sda" ];
96-
efiSupport = true;
97-
efiInstallAsRemovable = true;
98-
};
99-
services.openssh.enable = true;
100-
system.stateVersion = "23.11";
101-
#-------------------------------------------------------
102-
# Change the line below replacing <insert your key here>
103-
# with your own ssh public key
104-
#-------------------------------------------------------
105-
users.users.root.openssh.authorizedKeys.keys = [ "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJV/MZW0GP6guibA1rNwPwK6Q0WGg1of6MQRMpeqiUR8 mahene" ];
106-
})
105+
services.openssh.enable = true;
106+
system.stateVersion = "23.11";
107+
#-------------------------------------------------------
108+
# Change the line below replacing <insert your key here>
109+
# with your own ssh public key
110+
#-------------------------------------------------------
111+
users.users.root.openssh.authorizedKeys.keys = [
112+
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJV/MZW0GP6guibA1rNwPwK6Q0WGg1of6MQRMpeqiUR8 mahene"
107113
];
108-
};
114+
})
115+
];
116+
};
109117
};
110-
# // dream2nix.lib.makeFlakeOutputs {
111-
# inherit systems;
112-
# config.projectRoot = ./.;
113-
# source =
114-
# lib.sourceFilesBySuffices ./. [ ".rs" "Cargo.toml" "Cargo.lock" ];
115-
# projects."rust-nixos" = {
116-
# name = "2rust-nixos";
117-
# translator = "cargo-lock";
118-
# };
119-
# };
118+
# // dream2nix.lib.makeFlakeOutputs {
119+
# inherit systems;
120+
# config.projectRoot = ./.;
121+
# source =
122+
# lib.sourceFilesBySuffices ./. [ ".rs" "Cargo.toml" "Cargo.lock" ];
123+
# projects."rust-nixos" = {
124+
# name = "2rust-nixos";
125+
# translator = "cargo-lock";
126+
# };
127+
# };
120128
}

module.nix

+32-34
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ in {
1414

1515
# never change once deployed
1616
databaseName = mkOption {
17-
type = types.str;
18-
default = "rustnixos";
19-
description = ''database name, also a db user and Linux user.
20-
Internal since changing this value would lead to breakage while setting up databases'';
21-
internal = true;
22-
readOnly = true;
17+
type = types.str;
18+
default = "rustnixos";
19+
description = ''
20+
database name, also a db user and Linux user.
21+
Internal since changing this value would lead to breakage while setting up databases'';
22+
internal = true;
23+
readOnly = true;
2324
};
2425

2526
host = mkOption rec {
@@ -52,34 +53,30 @@ in {
5253
description = "My app service user";
5354
isSystemUser = true;
5455
};
55-
users.groups.${cfg.databaseName} = {};
56-
56+
users.groups.${cfg.databaseName} = { };
5757
services.postgresql = {
58-
enable = true;
59-
# only local unix sockets
60-
enableTCPIP = false;
61-
# v15 doesn't work yet in NixOS. See https://github.com/NixOS/nixpkgs/issues/216989.
62-
# package = pkgs.postgresql_15;
63-
# package = pkgs.postgresql_14;
64-
ensureDatabases = [ cfg.databaseName ];
65-
# create a DB user/role (not a Linux user!) of the same name
66-
ensureUsers = [
67-
{
68-
name = cfg.databaseName;
69-
ensurePermissions = {
70-
"DATABASE ${cfg.databaseName}" = "ALL PRIVILEGES";
71-
# "SCHEMA public" = "ALL PRIVILEGES,CREATE";
72-
};
73-
}];
74-
75-
authentication = pkgs.lib.mkOverride 10 ''
76-
local sameuser all peer
77-
'';
78-
};
79-
# backup all databases automatically
80-
services.postgresqlBackup = {
8158
enable = true;
59+
# only local unix sockets
60+
enableTCPIP = false;
61+
# v15 doesn't work yet in NixOS. See https://github.com/NixOS/nixpkgs/issues/216989.
62+
# package = pkgs.postgresql_15;
63+
# package = pkgs.postgresql_14;
64+
ensureDatabases = [ cfg.databaseName ];
65+
# create a DB user/role (not a Linux user!) of the same name
66+
ensureUsers = [{
67+
name = cfg.databaseName;
68+
ensurePermissions = {
69+
"DATABASE ${cfg.databaseName}" = "ALL PRIVILEGES";
70+
# "SCHEMA public" = "ALL PRIVILEGES,CREATE";
71+
};
72+
}];
73+
74+
authentication = pkgs.lib.mkOverride 10 ''
75+
local sameuser all peer
76+
'';
8277
};
78+
# backup all databases automatically
79+
services.postgresqlBackup = { enable = true; };
8380

8481
systemd.services = {
8582

@@ -90,7 +87,8 @@ in {
9087
requires = [ "postgresql.service" ];
9188

9289
environment = {
93-
DATABASE_URL = "postgres:///${cfg.databaseName}?socket=/var/run/postgresql";
90+
DATABASE_URL =
91+
"postgres:///${cfg.databaseName}?socket=/var/run/postgresql";
9492
};
9593

9694
serviceConfig = {
@@ -100,8 +98,8 @@ in {
10098
# which runs this service on every reboot.
10199
# which is what we want.
102100
ExecStart =
103-
# Don' use "dbmate .. up, because it will try to create a database as DB user postgres,
104-
# but we don't allow this services Linux user to connect as postgres superuser/admin for security.
101+
# Don' use "dbmate .. up, because it will try to create a database as DB user postgres,
102+
# but we don't allow this services Linux user to connect as postgres superuser/admin for security.
105103
"${pkgs.dbmate}/bin/dbmate -d ${migration-data} --no-dump-schema migrate";
106104
};
107105
};

nix/caddy.module.nix

+32-28
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
11
({ pkgs, config, ... }: {
2-
networking.firewall.allowedTCPPorts = [ 80 443 ];
3-
services.rustnixos.enable = true;
2+
networking.firewall.allowedTCPPorts = [ 80 443 ];
3+
services.rustnixos.enable = true;
44

5-
services = {
6-
caddy = {
7-
enable = true;
8-
# acmeCA =
9-
# "https://acme-staging-v02.api.letsencrypt.org/directory";
10-
globalConfig = ''
11-
# debug
12-
# auto_https disable_certs
13-
skip_install_trust
14-
# http_port 8080
15-
# https_port 8090
5+
services = {
6+
caddy = {
7+
enable = true;
8+
# acmeCA =
9+
# "https://acme-staging-v02.api.letsencrypt.org/directory";
10+
globalConfig = ''
11+
# debug
12+
# auto_https disable_certs
13+
skip_install_trust
14+
# http_port 8080
15+
# https_port 8090
1616
17-
'';
18-
# Test with curl
19-
# curl --connect-to localhost:80:mycontainer:80 --connect-to localhost:443:mycontainer:443 http://localhost -k -L
20-
virtualHosts = {
21-
"localhost".extraConfig = ''
22-
# respond "Hello, world34!"
23-
reverse_proxy http://127.0.0.1:${toString config.services.rustnixos.port}
24-
'';
25-
"workler.de".extraConfig = ''
26-
reverse_proxy http://127.0.0.1:${toString config.services.rustnixos.port}
27-
'';
28-
};
29-
};
30-
};
31-
})
17+
'';
18+
# Test with curl
19+
# curl --connect-to localhost:80:mycontainer:80 --connect-to localhost:443:mycontainer:443 http://localhost -k -L
20+
virtualHosts = {
21+
"localhost".extraConfig = ''
22+
# respond "Hello, world34!"
23+
reverse_proxy http://127.0.0.1:${
24+
toString config.services.rustnixos.port
25+
}
26+
'';
27+
"workler.de".extraConfig = ''
28+
reverse_proxy http://127.0.0.1:${
29+
toString config.services.rustnixos.port
30+
}
31+
'';
32+
};
33+
};
34+
};
35+
})

nix/disk-config.disko.nix

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
{ disks ? [ "/dev/sda" ], ... }:
2-
{
1+
{ disks ? [ "/dev/sda" ], ... }: {
32
disk = {
43
main = {
54
type = "disk";
@@ -39,4 +38,4 @@
3938
};
4039
};
4140
};
42-
}
41+
}

nix/migration-data.package.nix

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Bundle sql scripts as a Nix package so that it can be deployed into a server.
2-
{runCommand, ...}:
2+
{ runCommand, ... }:
33
runCommand "migration-data" { } ''
44
mkdir $out
55
cp -r ${../db/migrations}/*.sql $out
6-
''
6+
''

0 commit comments

Comments
 (0)