aboutsummaryrefslogtreecommitdiff
path: root/flake.nix
blob: 10b7b4fb3f58c20f77ecf13c3cd11ef84b51cc74 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-25.11";
    flake-utils.url = "github:numtide/flake-utils/main";

    rust-overlay = {
      url = "github:oxalica/rust-overlay/master";
      inputs.nixpkgs.follows = "nixpkgs";
    };

    crane.url = "github:ipetkov/crane";
  };

  description = "thulani discord bot";

  outputs = {
    self,
    nixpkgs,
    flake-utils,
    ...
  } @ inputs: let
    inherit (nixpkgs) lib;
    mkBuildToolchain = pkgs: pkgs.rust-bin.nightly."2025-12-01".minimal;

    rustsrc = let
      filter = with lib.fileset; unions [
        (maybeMissing ./.cargo/config.toml)
        ./README.md
        ./Cargo.lock
        (fileFilter (file: file.hasExt "rs") ./.)
        (fileFilter (file: file.hasExt "sql") ./.)
        (fileFilter (file: file.hasExt "pest") ./.)
        (fileFilter (file: file.name == "Cargo.toml") ./.)
      ];
    in lib.fileset.toSource {
      root = ./.;
      fileset = filter;
    };

    deps = pkgs: with pkgs; [
      openssl
      pkg-config
      libopus
      postgresql
    ];

    mkPkg = pkgs: let
      buildToolchain = mkBuildToolchain pkgs;
      crane = (inputs.crane.mkLib pkgs).overrideToolchain mkBuildToolchain;

      inherit (pkgs) lib;

    in pkgs.callPackage ./nix/package.nix {
      inherit crane rustsrc;
    };

  in (flake-utils.lib.eachDefaultSystem (system:
    let
      pkgs = import nixpkgs {
        inherit system;

        overlays = [
          (import inputs.rust-overlay)
        ];
      };

      dbInit = (pkgs.writeShellScript "thulani-dbinit" (builtins.readFile ./nix/dbinit.sh)).overrideAttrs (final: prev: {
        checkPhase = ''
          ${pkgs.shellcheck}/bin/shellcheck $out
        '';
      });

      devToolchain = (mkBuildToolchain pkgs).override {
        extensions = [
          "rust-src"
          "rust-analyzer"
          "clippy"
          "rust-docs"
          "rustfmt"
        ];
      };

      pkg = mkPkg pkgs;

    in {
      devShells.default = pkgs.mkShell {
        buildInputs = (with pkgs; [
          devToolchain
          shellcheck
        ]) ++ (deps pkgs);

        RUST_SRC_PATH = "${devToolchain}/lib/rustlib/src/rust";
      };

      packages = {
        default = pkg;
        inherit dbInit;
      };

      apps.default = {
        type = "app";
        program = "${pkg}/bin/thulani";
      };
    }) // {
      hydraJobs = {
        inherit (self.packages) x86_64-linux;
      };

      overlays.default = final: prev: let
        withRust = ((import inputs.rust-overlay) final prev);
      in withRust // {
        thulani = mkPkg (final // withRust);
      };

      nixosConfigurations.test = nixpkgs.lib.nixosSystem {
        system = "x86_64-linux";

        modules = [
          self.nixosModules.default
          "${nixpkgs}/nixos/modules/virtualisation/qemu-vm.nix"

          ({ config, lib, pkgs, ... }: {
            nixpkgs.overlays = [
              self.overlays.default
            ];

            environment.systemPackages = with pkgs; [
              (writeShellScriptBin "jl" ''
                journalctl -eu thulani | cat
              '')

              (writeShellScriptBin "s" ''
                exec systemctl status thulani $@
              '')
            ];

            users = {
              mutableUsers = false;

              users.root.hashedPassword = lib.mkForce "";

              users.test = {
                password = lib.mkForce "test";
                group = "test";
                isNormalUser = true;
                extraGroups = [
                  "wheel"
                  "sudo"
                ];
              };

              groups.test = {};
            };

            security.sudo.wheelNeedsPassword = false;

            virtualisation = {
              cores = 8;
              graphics = false;
              diskSize = 32*1024;
              memorySize = 12*1024;
              writableStoreUseTmpfs = false;
            };

            services.thulani = {
              enable = true;

              environment = {
                STEAM_API_KEY = "";
                SHEETS_API_KEY = "";
                SPREADSHEET_ID = "";
                THULANI_CLIENT_ID = "1";
                THULANI_TOKEN = "";
                VOICE_CHANNEL = "1";
                OWNER_ID = "1";
                TARGET_GUILD = "1";
              };

              postgres = {
                enable = true;
              };
            };
          })
        ];
      };

      nixosModules.default = import ./nix/module.nix self;
    }
  );
}