This commit is contained in:
Rachel Lambda Samuelsson 2024-05-25 01:01:07 +02:00
parent 70c96d504d
commit d77da1ff1a
6 changed files with 38 additions and 6 deletions

View File

@ -10,11 +10,12 @@ let services = with builtins;
secrets = import ./secrets/secrets.nix; secrets = import ./secrets/secrets.nix;
hostIp = "10.10.0.1"; hostIp = "10.10.0.1";
hostVolumeDir = "/var/lib/container-storage/";
in in
{ {
containers = containers =
lib.foldMap ({ name, config, ip, ports, ... }: lib.flatMap ({ name, config, ip, ports, volumes, ... }:
{ {
${name} = { ${name} = {
autoStart = true; autoStart = true;
@ -22,13 +23,22 @@ lib.foldMap ({ name, config, ip, ports, ... }:
privateNetwork = true; privateNetwork = true;
hostAddress = hostIp; hostAddress = hostIp;
localAddress = ip; localAddress = ip;
bindMounts = lib.flatMap (volume@{ name, mountPoint }:
{
"${name}" = {
inherit mountPoint;
isReadOnly = if volume ? readOnly then volume.readOnly else false;
hostPath = hostVolumeDir + name;
};
}
) volumes;
config = config // { config = config // {
boot.isContainer = true; boot.isContainer = true;
networking = { networking = {
hostName = "${name}"; hostName = "${name}";
hosts = lib.foldMap ({ name, ip, ...}: hosts = lib.flatMap ({ name, ip, ...}:
{ "${ip}" = [ "${name}.containers" "${name}" ]; } { "${ip}" = [ "${name}.containers" "${name}" ]; }
) services; ) services;
@ -47,13 +57,20 @@ lib.foldMap ({ name, config, ip, ports, ... }:
// //
{ {
system.activationScripts.makeBindMounts = with builtins;
lib.flatMapS (name: ''
mkdir -p ${hostVolumeDir + name}
'')
(concatMap (s: map (v: v.name) s.volumes) services);
# [NGINX] # [NGINX]
services.nginx = { services.nginx = {
enable = true; enable = true;
recommendedProxySettings = true; recommendedProxySettings = true;
virtualHosts = virtualHosts =
lib.foldMap ({ ports, hosts, ip, ... }: lib.flatMap ({ ports, hosts, ip, ... }:
lib.foldMap (host: lib.flatMap (host:
if (builtins.isNull ports.http) if (builtins.isNull ports.http)
then {} then {}
else { else {

View File

@ -1,3 +1,4 @@
{ {
foldMap = (f: list: builtins.foldl' (acc: elem: acc // elem) {} (builtins.map f list)); flatMap = (f: list: builtins.foldl' (acc: elem: acc // elem) {} (builtins.map f list));
flatMapS = (f: list: builtins.foldl' (acc: elem: acc + elem) "" (builtins.map f list));
} }

1
result
View File

@ -1 +0,0 @@
/nix/store/cy1mab4p2g1zf38bcdpb3ix034wxs8a8-nixos-vm

View File

@ -26,4 +26,8 @@
http = 3001; http = 3001;
}; };
hosts = [ "localhost" ]; hosts = [ "localhost" ];
volumes = [{
name = "gitea-statedir";
mountPoint = "/var/lib/gitea";
}]; # TODO
} }

View File

@ -21,4 +21,8 @@
http = null; http = null;
}; };
hosts = [ ]; hosts = [ ];
volumes = [{
name = "postgres-storage";
mountPoint = "/var/lib/postgresql";
}];
} }

View File

@ -15,5 +15,12 @@ Services are of the form:
http = 80; http = 80;
}; };
hosts = [ "myservice.domain.mjau" ]; hosts = [ "myservice.domain.mjau" ];
volumes = [
{
name = "myservice-storage";
mountPoint = "/var/lib/whatever";
readOnly = false;
};
];
} }
``` ```