2024-03-14 12:53:32 +05:00
|
|
|
{
|
2024-07-10 01:23:07 +05:00
|
|
|
description = "Oscuro - a fancy discord bot";
|
2024-03-14 12:53:32 +05:00
|
|
|
|
2024-07-10 01:23:07 +05:00
|
|
|
nixConfig = {
|
|
|
|
extra-substituters = ["https://bonfire.cachix.org"];
|
|
|
|
extra-trusted-public-keys = ["bonfire.cachix.org-1:mzAGBy/Crdf8NhKail5ciK7ZrGRbPJJobW6TwFb7WYM="];
|
|
|
|
};
|
2024-04-26 16:44:22 +05:00
|
|
|
|
2024-07-10 01:23:07 +05:00
|
|
|
inputs = {
|
|
|
|
bonfire = {
|
|
|
|
url = "github:L-Nafaryus/bonfire";
|
2024-04-26 16:44:22 +05:00
|
|
|
};
|
2024-07-10 01:23:07 +05:00
|
|
|
nixpkgs.follows = "bonfire/nixpkgs";
|
|
|
|
};
|
|
|
|
|
|
|
|
outputs = {
|
|
|
|
self,
|
|
|
|
nixpkgs,
|
|
|
|
bonfire,
|
|
|
|
...
|
|
|
|
}: let
|
|
|
|
forAllSystems = nixpkgs.lib.genAttrs ["x86_64-linux"];
|
|
|
|
nixpkgsFor = forAllSystems (system: import nixpkgs {inherit system;});
|
|
|
|
in {
|
|
|
|
packages = forAllSystems (system: let
|
|
|
|
pkgs = nixpkgsFor.${system};
|
|
|
|
crane-lib = bonfire.inputs.crane.lib.${system};
|
|
|
|
|
|
|
|
src = pkgs.lib.cleanSourceWith {
|
|
|
|
src = ./.;
|
|
|
|
filter = path: type: (crane-lib.filterCargoSources path type);
|
|
|
|
};
|
|
|
|
|
|
|
|
common = {
|
|
|
|
inherit src;
|
|
|
|
pname = "oscuro";
|
|
|
|
version = "0.1.0";
|
|
|
|
strictDeps = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
cargoArtifacts = crane-lib.buildDepsOnly common;
|
|
|
|
in {
|
|
|
|
oscuro = crane-lib.buildPackage (common // {inherit cargoArtifacts;});
|
|
|
|
|
|
|
|
default = self.packages.${system}.oscuro;
|
|
|
|
});
|
|
|
|
|
|
|
|
devShells = forAllSystems (system: let
|
|
|
|
pkgs = nixpkgsFor.${system};
|
|
|
|
bonfire-pkgs = bonfire.packages.${system};
|
|
|
|
fenix-pkgs = bonfire.inputs.fenix.packages.${system};
|
|
|
|
in {
|
|
|
|
default = pkgs.mkShell {
|
|
|
|
buildInputs = [
|
|
|
|
fenix-pkgs.complete.toolchain
|
|
|
|
bonfire-pkgs.cargo-shuttle
|
|
|
|
pkgs.cargo-release
|
|
|
|
pkgs.pkg-config
|
|
|
|
pkgs.openssl
|
|
|
|
];
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
nixosModules = {
|
|
|
|
oscuro = {
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}:
|
|
|
|
with lib; let
|
|
|
|
cfg = config.services.oscuro;
|
|
|
|
opt = options.services.oscuro;
|
|
|
|
pkg = self.packages.${pkgs.system}.oscuro;
|
|
|
|
configFile = pkgs.writeText "config.toml" ''
|
|
|
|
discord_token = "#discord_token#"
|
|
|
|
'';
|
|
|
|
in {
|
|
|
|
options.services.oscuro = {
|
|
|
|
enable = mkEnableOption "Enables the Oscuro bot";
|
2024-03-14 12:53:32 +05:00
|
|
|
|
2024-07-10 01:23:07 +05:00
|
|
|
package = mkPackageOption pkgs "oscuro" {};
|
2024-04-26 16:44:22 +05:00
|
|
|
|
2024-07-10 01:23:07 +05:00
|
|
|
dataDir = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/var/lib/oscuro";
|
|
|
|
description = lib.mdDoc "Directory to store Oscuro files";
|
2024-04-26 16:44:22 +05:00
|
|
|
};
|
|
|
|
|
2024-07-10 01:23:07 +05:00
|
|
|
discordToken = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
example = "Bot TOKENTOKENTOKEN";
|
|
|
|
};
|
2024-04-26 16:44:22 +05:00
|
|
|
|
2024-07-10 01:23:07 +05:00
|
|
|
discordTokenFile = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
example = "/var/lib/secrets/oscuro/discord_token";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = cfg.discordToken != null || cfg.discordTokenFile != null;
|
|
|
|
message = "Discord token must be set. Use `services.oscuro.discordToken` or `services.oscuro.discordTokenFile`.";
|
|
|
|
}
|
|
|
|
];
|
2024-04-26 16:44:22 +05:00
|
|
|
|
2024-07-10 01:23:07 +05:00
|
|
|
users.users.oscuro = {
|
|
|
|
description = "Oscuro bot service user";
|
|
|
|
home = cfg.dataDir;
|
|
|
|
createHome = true;
|
|
|
|
isSystemUser = true;
|
|
|
|
group = "oscuro";
|
2024-04-26 16:44:22 +05:00
|
|
|
};
|
2024-07-10 01:23:07 +05:00
|
|
|
users.groups.oscuro = {};
|
|
|
|
|
|
|
|
systemd.services.oscuro = {
|
|
|
|
description = "Oscuro";
|
|
|
|
wantedBy = ["multi-user.target"];
|
|
|
|
after = ["network.target"];
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
Restart = "always";
|
|
|
|
ExecStart = "${pkg}/bin/oscuro";
|
|
|
|
User = "oscuro";
|
|
|
|
WorkingDirectory = cfg.dataDir;
|
|
|
|
};
|
|
|
|
|
|
|
|
preStart = let
|
|
|
|
runConfig = "${cfg.dataDir}/config.toml";
|
|
|
|
replaceSecret = "${pkgs.replace-secret}/bin/replace-secret";
|
|
|
|
in ''
|
|
|
|
cp -f '${configFile}' '${runConfig}'
|
|
|
|
chmod u+w '${runConfig}'
|
|
|
|
|
|
|
|
${lib.optionalString (cfg.discordTokenFile != null) ''
|
|
|
|
${replaceSecret} '#discord_token#' '${cfg.discordTokenFile}' '${runConfig}'
|
|
|
|
''}
|
|
|
|
${lib.optionalString (cfg.discordToken != null) ''
|
|
|
|
sed -i 's/#discord_token#/${cfg.discordToken}/g' '${runConfig}'
|
|
|
|
''}
|
|
|
|
|
|
|
|
'';
|
2024-04-26 16:44:22 +05:00
|
|
|
};
|
2024-07-10 01:23:07 +05:00
|
|
|
};
|
2024-03-14 12:53:32 +05:00
|
|
|
};
|
|
|
|
|
2024-07-10 01:23:07 +05:00
|
|
|
default = self.nixosModules.oscuro;
|
|
|
|
};
|
2024-04-26 16:44:22 +05:00
|
|
|
|
2024-07-10 01:23:07 +05:00
|
|
|
nixosConfigurations.oscuro = nixpkgs.lib.nixosSystem {
|
|
|
|
system = "x86_64-linux";
|
|
|
|
modules = [
|
|
|
|
self.nixosModules.oscuro
|
|
|
|
({pkgs, ...}: {
|
|
|
|
boot.isContainer = true;
|
2024-04-26 16:44:22 +05:00
|
|
|
|
2024-07-10 01:23:07 +05:00
|
|
|
networking.hostName = "oscuro";
|
|
|
|
networking.useDHCP = false;
|
2024-04-26 16:44:22 +05:00
|
|
|
|
2024-07-10 01:23:07 +05:00
|
|
|
services.oscuro = {
|
|
|
|
enable = true;
|
|
|
|
discordToken = ""; # insert token
|
|
|
|
};
|
2024-03-14 12:53:32 +05:00
|
|
|
|
2024-07-10 01:23:07 +05:00
|
|
|
system.stateVersion = "24.05";
|
|
|
|
})
|
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
2024-03-14 12:53:32 +05:00
|
|
|
}
|