init flake checks

This commit is contained in:
raf 2023-11-29 02:02:15 +03:00
parent 000b7b1c9a
commit 87ae83c193
No known key found for this signature in database
GPG key ID: 02D1DD3FA08B6B29
3 changed files with 81 additions and 11 deletions

View file

@ -22,15 +22,22 @@
formatter = pkgs.alejandra; formatter = pkgs.alejandra;
packages = { packages = {
pi-air-quality-monitor = pkgs.callPackage ./nix/default.nix {};
default = self'.packages.pi-air-quality-monitor; default = self'.packages.pi-air-quality-monitor;
pi-air-quality-monitor = pkgs.callPackage ./nix/default.nix {};
};
devShells = {
default = self'.packages.pi-air-quality-monitor;
pi-air-quality-monitor = pkgs.mkShell {
inputsFrom = self'.packages.pi-air-quality-monitor.pythonPath;
};
}; };
}; };
flake = { flake = {
nixosModules = { nixosModules = {
pi-air-quality-monitor = import ./nix/module.nix self;
default = self.nixosModules.pi-air-quality-monitor; default = self.nixosModules.pi-air-quality-monitor;
pi-air-quality-monitor = import ./nix/module.nix self;
}; };
}; };
}; };

View file

@ -4,7 +4,7 @@ self: {
lib, lib,
... ...
}: let }: let
inherit (lib) mkEnableOption mkOption mkIf types; inherit (lib) mkEnableOption mkOption mkIf types optional;
cfg = config.services.pi-air-quality-monitor; cfg = config.services.pi-air-quality-monitor;
in { in {
@ -38,23 +38,85 @@ in {
default = "pi-aqm"; default = "pi-aqm";
description = "Group to run the server as"; description = "Group to run the server as";
}; };
device = mkOption {
type = types.path;
default = "/dev/ttyUSB0";
description = "Device to read data from";
};
environmentFile = mkOption {
type = with types; nullOr path;
default = null;
example = "/etc/pi-aqm.env";
description = "File to read environment variables from";
};
dataDir = mkOption {
type = types.path;
default = "/var/lib/pi-aqm";
description = "Directory to store data in";
};
redis = {
createLocally = mkOption {
type = types.bool;
default = true;
description = "Whether to create a Redis instance locally";
};
host = mkOption {
type = types.str;
default = "localhost";
description = "Redis host";
};
port = mkOption {
type = types.int;
default = 6379;
description = "Redis port";
};
redis_db = mkOption {
type = types.int;
default = 0;
description = "Redis database";
};
};
}; };
}; };
config = mkIf config.services.pi-air-quality-monitor.enable { config = mkIf config.services.pi-air-quality-monitor.enable {
networking.firewall.allowedTCPPorts = [cfg.settings.port]; networking.firewall.allowedTCPPorts = [cfg.settings.port];
users = { users = {
users.pi-aqm = { groups."${cfg.settings.group}" = {};
users."${cfg.settings.user}" = {
isSystemUser = true; isSystemUser = true;
group = "pi-aqm"; group = "${cfg.settings.group}";
home = "/var/lib/pi-aqm"; home = "${cfg.settings.dataDir}";
createHome = true; createHome = true;
}; };
groups.pi-aqm = {};
}; };
systemd.services."pi-air-quality-monitor" = { services.redis = mkIf cfg.settings.redis.createLocally {
servers = {
pi-aqm = {
enable = true;
user = "pi-aqm";
databases = 16;
logLevel = "debug";
inherit (cfg.settings.redis) port;
};
};
};
systemd.services."pi-air-quality-monitor" = let
redisEnv = pkgs.writeText "redis.env" ''
REDIS_HOST=${cfg.settings.redis.host}
REDIS_PORT=${toString cfg.settings.redis.port}
REDIS_DB=${toString cfg.settings.redis.redis_db}
'';
in {
description = "An air quality monitoring service with a Raspberry Pi and a SDS011 sensor"; description = "An air quality monitoring service with a Raspberry Pi and a SDS011 sensor";
wantedBy = ["multi-user.target"]; wantedBy = ["multi-user.target"];
after = ["network.target"]; after = ["network.target"];
@ -62,7 +124,8 @@ in {
Type = "simple"; Type = "simple";
User = cfg.settings.user; User = cfg.settings.user;
Group = cfg.settings.group; Group = cfg.settings.group;
WorkingDirectory = "/var/lib/pi-aqm"; EnvironmentFile = [redisEnv] ++ optional (cfg.settings.environmentFile != null) cfg.settings.environmentFile;
WorkingDirectory = "${cfg.settings.dataDir}";
ExecStart = "${lib.getExe cfg.package}"; ExecStart = "${lib.getExe cfg.package}";
Restart = "always"; Restart = "always";
}; };

View file

@ -47,7 +47,7 @@ nixosTest {
testScript = '' testScript = ''
server.wait_for_unit("default.target") server.wait_for_unit("default.target")
server.succeed("ls -lah /dev/ttyUSB0") server.succeed("ls -lah /dev/ttyUSB0")
server.succeed('systemctl status pi-air-quality-monitor | grep \"Active: active (running)\" || return 0') #server.succeed('systemctl status pi-air-quality-monitor | grep \"Active: active (running)\" || return 0')
#server.succeed('nc -vz server 8080') #server.succeed('nc -vz server 8080')
#client.wait_for_unit("default.target") #client.wait_for_unit("default.target")