aboutsummaryrefslogtreecommitdiff
path: root/flake.nix
blob: b7fafaefdf89b505cb21d6d708509ce4ef080f03 (plain)
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
{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
  };

  outputs = {
    self,
    nixpkgs,
  }: let
    supportedSystems = ["x86_64-linux" "aarch64-linux" "aarch64-darwin"];
    forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
    nixpkgsFor = forAllSystems (system:
      import nixpkgs {
        inherit system;
        overlays = [
          self.overlays.default
        ];
      });
  in {
    overlays.default = final: prev: let
      pname = "lurker";
      version = "0.1.0";
    in {
      node_modules = with final;
        stdenv.mkDerivation {
          pname = "lurker-node-modules";
          version = "0.0.1";
          impureEnvVars =
            lib.fetchers.proxyImpureEnvVars
            ++ ["GIT_PROXY_COMMAND" "SOCKS_SERVER"];
          src = ./.;
          nativeBuildInputs = [bun];
          buildInputs = [nodejs-slim_latest];
          dontConfigure = true;
          dontFixup = true;
          buildPhase = ''
            bun install --no-progress --frozen-lockfile
          '';
          installPhase = ''
            mkdir -p $out/node_modules
            cp -R ./node_modules/* $out/node_modules
            ls -la $out/node_modules
          '';
          outputHash = "sha256-XX8RozzH76qzBAIyWYwPynaxGIaHNojNw5s6aCqaSio=";
          outputHashAlgo = "sha256";
          outputHashMode = "recursive";
        };
      lurker = with final;
        stdenv.mkDerivation {
          inherit pname version;
          src = ./.;
          nativeBuildInputs = [makeBinaryWrapper];
          buildInputs = [bun];

          buildPhase = ''
            runHook preBuild
            runHook postBuild
          '';

          dontFixup = true;

          installPhase = ''
            runHook preInstall

            mkdir -p $out/bin

            ln -s ${node_modules}/node_modules $out
            cp -R ./* $out

            makeBinaryWrapper ${bun}/bin/bun $out/bin/$pname \
            --prefix PATH : ${lib.makeBinPath [bun]} \
            --add-flags "run --prefer-offline --no-install $out/src/index.js"

          '';
        };
      dockerImage = with final;
        final.dockerTools.buildImage {
          name = pname;
          tag = "latest";

          copyToRoot = final.buildEnv {
            name = "image-root";
            paths = [final.lurker];
            pathsToLink = ["/bin"];
          };

          runAsRoot = ''
            mkdir -p /data
          '';

          config = {
            Cmd = ["/bin/${pname}"];
            WorkingDir = "/data";
            Volumes = {"/data" = {};};
          };
        };
    };

    devShell = forAllSystems (system: let
      pkgs = nixpkgsFor."${system}";
    in
      pkgs.mkShell {
        nativeBuildInputs = [
          pkgs.bun
          pkgs.biome
          pkgs.typescript-language-server
        ];
      });

    packages = forAllSystems (system: {
      inherit (nixpkgsFor."${system}") lurker node_modules dockerImage;
    });

    defaultPackage = forAllSystems (system: nixpkgsFor."${system}".lurker);

    apps = forAllSystems (system: let
      pkgs = nixpkgsFor.${system};
    in {
      default = {
        type = "app";
        program = "${pkgs.lurker}/bin/lurker";
      };
    });

    formatter = forAllSystems (system: nixpkgsFor."${system}".alejandra);

    nixosModules.default = {
      config,
      pkgs,
      lib,
      ...
    }:
      with lib; {
        options = {
          services.lurker = {
            enable = mkOption {
              type = types.bool;
              default = false;
              description = "Enable lurker";
            };
            port = mkOption {
              type = types.int;
              default = 3000;
              description = "Port to run lurker on";
            };
          };
        };

        config = mkIf config.services.lurker.enable {
          nixpkgs.overlays = [self.overlays.default];
          systemd.services.lurker = {
            description = "lurker service";
            wantedBy = ["multi-user.target"];

            serviceConfig = {
              ListenStream = "0.0.0.0:${toString config.services.lurker.port}";
              ExecStart = "${pkgs.lurker}/bin/lurker";
              Restart = "always";
            };

            # If the binary needs specific environment variables, set them here
            environment = {
              LURKER_PORT = "${toString config.services.lurker.port}";
            };
          };
        };
      };
  };
}