2023-06-18 19:46:02 +00:00
|
|
|
{
|
|
|
|
description = "Skynet LDAP backend";
|
|
|
|
|
|
|
|
inputs = {
|
|
|
|
nixpkgs.url = "nixpkgs/nixos-23.05";
|
|
|
|
naersk.url = "github:nix-community/naersk";
|
|
|
|
utils.url = "github:numtide/flake-utils";
|
|
|
|
};
|
|
|
|
|
|
|
|
outputs = { self, nixpkgs, utils, naersk }: utils.lib.eachDefaultSystem (system:
|
|
|
|
let
|
|
|
|
pkgs = nixpkgs.legacyPackages."${system}";
|
|
|
|
naersk-lib = naersk.lib."${system}";
|
|
|
|
package_name = "skynet_ldap_backend";
|
|
|
|
package_update = "update_groups";
|
|
|
|
desc = "Skynet LDAP backend";
|
|
|
|
in rec {
|
|
|
|
|
|
|
|
# `nix build`
|
|
|
|
packages."${package_name}" = naersk-lib.buildPackage {
|
|
|
|
pname = "${package_name}";
|
|
|
|
root = ./.;
|
|
|
|
|
|
|
|
buildInputs = [
|
|
|
|
pkgs.openssl
|
|
|
|
pkgs.pkg-config
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
defaultPackage = packages."${package_name}";
|
|
|
|
|
|
|
|
# `nix run`
|
|
|
|
apps."${package_name}" = utils.lib.mkApp {
|
|
|
|
drv = packages."${package_name}";
|
|
|
|
};
|
|
|
|
|
|
|
|
defaultApp = apps."${package_name}";
|
|
|
|
|
|
|
|
# `nix develop`
|
|
|
|
devShell = pkgs.mkShell {
|
2023-08-20 15:24:25 +00:00
|
|
|
nativeBuildInputs = with pkgs; [ rustc cargo pkg-config openssl];
|
2023-06-18 19:46:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
nixosModule = { lib, pkgs, config, ... }:
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.services."${package_name}";
|
2023-07-30 03:29:37 +00:00
|
|
|
# secret options are in the env file loaded separately
|
|
|
|
environment_config = {
|
|
|
|
# basic server stuff
|
2023-07-30 04:54:45 +00:00
|
|
|
HOME = cfg.home;
|
|
|
|
DATABASE = "database.db";
|
|
|
|
CSV = "wolves.csv";
|
|
|
|
HOST_PORT = cfg.host_port;
|
2023-08-24 17:54:22 +00:00
|
|
|
SSH_ROOT = "skynet_old";
|
2023-06-18 19:46:02 +00:00
|
|
|
|
|
|
|
# special categories of users
|
2023-09-16 13:56:16 +00:00
|
|
|
USERS_ADMIN = lib.strings.concatStringsSep "," cfg.users.admin;
|
|
|
|
USERS_COMMITTEE = lib.strings.concatStringsSep "," cfg.users.committee;
|
|
|
|
USERS_LIFETIME = lib.strings.concatStringsSep "," cfg.users.lifetime;
|
|
|
|
USERS_BANNED = lib.strings.concatStringsSep "," cfg.users.banned;
|
|
|
|
USERS_RESTRICTED = lib.strings.concatStringsSep "," cfg.users.restricted;
|
2023-06-18 19:46:02 +00:00
|
|
|
};
|
2023-08-05 21:23:36 +00:00
|
|
|
|
2023-08-05 22:08:52 +00:00
|
|
|
service_name = script: lib.strings.sanitizeDerivationName("${cfg.user}@${script}");
|
2023-08-05 21:23:36 +00:00
|
|
|
|
|
|
|
# oneshot scripts to run
|
2023-08-05 21:57:55 +00:00
|
|
|
serviceGenerator = mapAttrs' (script: time: nameValuePair (service_name script) {
|
2023-08-05 21:23:36 +00:00
|
|
|
description = "Service for ${desc} ${script}";
|
|
|
|
wantedBy = [ ];
|
|
|
|
after = [ "network-online.target" ];
|
|
|
|
environment = environment_config;
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
2023-08-05 22:08:52 +00:00
|
|
|
User = "${cfg.user}";
|
|
|
|
Group = "${cfg.user}";
|
2023-08-05 21:23:36 +00:00
|
|
|
ExecStart = "${self.defaultPackage."${system}"}/bin/${script}";
|
2023-08-27 20:32:10 +00:00
|
|
|
EnvironmentFile = [
|
|
|
|
"${cfg.env.ldap}"
|
|
|
|
"${cfg.env.discord}"
|
2023-09-10 21:21:44 +00:00
|
|
|
"${cfg.env.mail}"
|
2023-08-27 20:32:10 +00:00
|
|
|
];
|
2023-08-05 21:23:36 +00:00
|
|
|
};
|
2023-08-05 21:57:55 +00:00
|
|
|
});
|
2023-08-05 21:23:36 +00:00
|
|
|
|
|
|
|
# each timer will run the above service
|
2023-08-05 21:57:55 +00:00
|
|
|
timerGenerator = mapAttrs' (script: time: nameValuePair (service_name script) {
|
2023-08-05 21:23:36 +00:00
|
|
|
description = "Timer for ${desc} ${script}";
|
|
|
|
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
partOf = [ "${service_name script}.service" ];
|
|
|
|
timerConfig = {
|
|
|
|
OnCalendar = time;
|
|
|
|
Unit = "${service_name script}.service";
|
|
|
|
Persistent = true;
|
|
|
|
};
|
2023-08-05 21:57:55 +00:00
|
|
|
});
|
2023-08-05 21:23:36 +00:00
|
|
|
|
|
|
|
# modify these
|
|
|
|
scripts = {
|
2023-08-06 13:27:24 +00:00
|
|
|
# every 15 min
|
2023-08-06 11:44:41 +00:00
|
|
|
"update_data" = "*:0,15,30,45";
|
2023-08-06 13:27:24 +00:00
|
|
|
#"new_users" = "*:5,20,35,50";
|
|
|
|
# groups are updated every 8 hours
|
|
|
|
"update_groups" = "00,08,16:00:00";
|
2023-08-05 21:23:36 +00:00
|
|
|
};
|
2023-08-05 21:27:54 +00:00
|
|
|
|
2023-06-18 19:46:02 +00:00
|
|
|
in {
|
|
|
|
options.services."${package_name}" = {
|
|
|
|
enable = mkEnableOption "enable ${package_name}";
|
|
|
|
|
|
|
|
# keep really secret stuff in this
|
2023-08-27 19:33:43 +00:00
|
|
|
env = {
|
|
|
|
ldap = mkOption rec {
|
|
|
|
type = types.str;
|
2023-09-10 21:21:44 +00:00
|
|
|
description = "Auth for the LDAP, has LDAP_HOST, LDAP_ADMIN, LDAP_ADMIN_PW";
|
2023-08-27 19:33:43 +00:00
|
|
|
};
|
|
|
|
discord = mkOption rec {
|
|
|
|
type = types.str;
|
|
|
|
description = "Auth for the discord bot, has LDAP_DISCORD_AUTH";
|
|
|
|
};
|
2023-09-10 21:21:44 +00:00
|
|
|
mail = mkOption rec {
|
2023-07-30 03:29:37 +00:00
|
|
|
type = types.str;
|
2023-09-10 21:21:44 +00:00
|
|
|
description = "Mail details, has EMAIL_SMTP, EMAIL_USER, EMAIL_PASS";
|
2023-07-30 03:29:37 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-06-18 19:46:02 +00:00
|
|
|
users = {
|
|
|
|
admin = mkOption rec {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
description = "array of admins";
|
|
|
|
};
|
|
|
|
committee = mkOption rec {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
description = "array of committee members";
|
|
|
|
};
|
|
|
|
lifetime = mkOption rec {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
description = "array of lifetime users";
|
|
|
|
};
|
|
|
|
banned = mkOption rec {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
description = "array of banned users";
|
|
|
|
};
|
2023-09-16 13:56:16 +00:00
|
|
|
restricted = mkOption rec {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
description = "array of restricted user accounts";
|
|
|
|
};
|
2023-06-18 19:46:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
host_port = mkOption rec {
|
|
|
|
type = types.str;
|
|
|
|
default = "127.0.0.1:8087";
|
|
|
|
description = "host/port for teh server tro run on";
|
|
|
|
};
|
|
|
|
|
|
|
|
# specific for teh program running
|
|
|
|
user = mkOption rec {
|
|
|
|
type = types.str;
|
|
|
|
default = "${package_name}";
|
|
|
|
description = "The user to run the service";
|
|
|
|
};
|
|
|
|
|
|
|
|
home = mkOption rec {
|
|
|
|
type = types.str;
|
2023-08-05 22:08:52 +00:00
|
|
|
default = "/etc/${cfg.prefix}${package_name}";
|
2023-06-18 19:46:02 +00:00
|
|
|
description = "The home for the user";
|
|
|
|
};
|
2023-08-05 21:23:36 +00:00
|
|
|
|
|
|
|
prefix = mkOption rec {
|
|
|
|
type = types.str;
|
|
|
|
default = "skynet_";
|
|
|
|
example = default;
|
|
|
|
description = "The prefix used to name service/folders";
|
|
|
|
};
|
2023-06-18 19:46:02 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
|
|
|
users.groups."${cfg.user}" = { };
|
|
|
|
|
|
|
|
users.users."${cfg.user}" = {
|
|
|
|
createHome = true;
|
|
|
|
isSystemUser = true;
|
|
|
|
home = "${cfg.home}";
|
|
|
|
group = "${cfg.user}";
|
|
|
|
};
|
|
|
|
|
2023-08-05 21:23:36 +00:00
|
|
|
systemd.services = {
|
|
|
|
# main service
|
|
|
|
"${cfg.user}" = {
|
|
|
|
description = desc;
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network-online.target" ];
|
|
|
|
wants = [ ];
|
|
|
|
environment = environment_config;
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
# because we are storing data we need a home for it
|
|
|
|
User = "${cfg.user}";
|
|
|
|
Group = "${cfg.user}";
|
|
|
|
Restart = "always";
|
|
|
|
ExecStart = "${self.defaultPackage."${system}"}/bin/${package_name}";
|
2023-08-27 19:33:43 +00:00
|
|
|
# multiple files
|
|
|
|
EnvironmentFile = [
|
|
|
|
"${cfg.env.ldap}"
|
|
|
|
"${cfg.env.discord}"
|
2023-09-10 21:21:44 +00:00
|
|
|
"${cfg.env.mail}"
|
2023-08-27 19:33:43 +00:00
|
|
|
];
|
2023-08-05 21:23:36 +00:00
|
|
|
};
|
2023-06-18 19:46:02 +00:00
|
|
|
};
|
2023-08-05 21:57:55 +00:00
|
|
|
} // serviceGenerator scripts;
|
2023-06-18 19:46:02 +00:00
|
|
|
|
2023-08-05 21:23:36 +00:00
|
|
|
# timers to run the above services
|
2023-08-05 21:57:55 +00:00
|
|
|
systemd.timers = timerGenerator scripts;
|
2023-07-30 03:29:37 +00:00
|
|
|
|
2023-06-18 19:46:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
});
|
2023-08-20 15:24:25 +00:00
|
|
|
}
|