summary refs log tree commit diff
path: root/default.nix
blob: 4b9dd756c8d9465a3439be5f9d622d9a640075ba (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
{ self, rVersion }:
{
  pkgs,
  lib,
  nodejs,
  ...
}:

let
  filteredSrc = lib.fileset.toSource {
    root = ./.;
    fileset = (
      lib.fileset.intersection ./. (
        lib.fileset.unions [
          ./src
          ./package.json
          ./tsconfig.json
          ./assets
          ./patches
          ./scripts
        ]
      )
    );
  };

  revsFile = pkgs.writeText "spacebar-server-rev.json" (
    builtins.toJSON {
      rev = self.sourceInfo.rev or self.sourceInfo.dirtyRev;
      shortRev = self.sourceInfo.shortRev or self.sourceInfo.dirtyShortRev;
      lastModified = self.sourceInfo.lastModified;
    }
  );

  srcHash = builtins.substring 11 32 (toString filteredSrc);
  unwrapped = pkgs.stdenv.mkDerivation {
    pname = "spacebar-server-ts-unwrapped";
    nodejs = pkgs.nodejs_24;
    version = "1.0.0-" + srcHash;

    meta = with lib; {
      description = "Spacebar server, a FOSS reimplementation of the Discord backend.";
      homepage = "https://github.com/spacebarchat/server";
      license = licenses.agpl3Plus;
      platforms = platforms.all;
      mainProgram = "start-bundle";
      maintainers = with maintainers; [ RorySys ];
    };

    src = filteredSrc;
    dontStrip = true;

    nativeBuildInputs = with pkgs; [
      nodejs
      (pkgs.python3.withPackages (ps: with ps; [ setuptools ]))
    ];

    configurePhase = ''
      cp -r --no-preserve=ownership,timestamps ${pkgs.callPackage ./node-modules.nix { }} node_modules
      chown $USER:$GROUP node_modules -R
      chmod +w node_modules -R
    '';

    buildPhase = ''
      npm run build:tsgo
    '';

    installPhase = ''
      runHook preInstall
      # set -x

      # remove packages not needed for production, or at least try to...
      npm prune --omit dev --no-save --offline
      rm -v dist/src.tsbuildinfo
      rm -rv scripts
      time ${./nix/trimNodeModules.sh}

      # Copy outputs
      echo "Installing package into $out"
      mkdir -p $out
      cp -r assets dist node_modules package.json $out/

      # set +x
      runHook postInstall
    '';
  };
in
pkgs.stdenv.mkDerivation {
  pname = "spacebar-server-ts";
  nodejs = unwrapped.nodejs;
  version = "1.0.0-" + rVersion;
  meta = unwrapped.meta;

  nativeBuildInputs = with pkgs; [
    makeWrapper
  ];

  # this isnt a real builder, we dont need these at all
  dontUnpack = true;
  dontBuild = true;
  dontPatch = true;
  dontConfigure = true;
  dontStrip = true;
  dontFixup = true;

  installPhase = ''
    # Copy outputs
    echo "Installing package into $out"
    mkdir -p $out
    cp -r --no-preserve=ownership,timestamps ${unwrapped}/. $out/

    # add version info
    cp ${revsFile} $out/.rev

    # Create wrappers for start scripts
    echo "Creating wrappers for start scripts"
    for i in $out/dist/**/start.js
    do
      makeWrapper ${pkgs.nodejs_24}/bin/node $out/bin/start-`dirname ''${i#$out/dist/}` --prefix NODE_PATH : $out/node_modules --add-flags --enable-source-maps --add-flags $i
    done
    makeWrapper ${pkgs.nodejs_24}/bin/node $out/bin/apply-migrations --prefix NODE_PATH : $out/node_modules --add-flags --enable-source-maps --add-flags $out/dist/apply-migrations.js
  '';

  passthru.tests = pkgs.runCommand "spacebar-server-ts-all-tests" rec {
    bundleStarts = pkgs.testers.runNixOSTest (import ./nix/tests/test-bundle-starts.nix { inherit self; });
    bundleStartsRabbitMqSingle = pkgs.testers.runNixOSTest (
      import ./nix/tests/test-bundle-starts.nix {
        inherit self;
        withIpc = "rabbitmq-single";
      }
    );
    bundleStartsRabbitMqLegacy = pkgs.testers.runNixOSTest (
      import ./nix/tests/test-bundle-starts.nix {
        inherit self;
        withIpc = "rabbitmq-legacy";
      }
    );

    nativeBuildInputs = [
      bundleStarts
      bundleStartsRabbitMqSingle
      bundleStartsRabbitMqLegacy
    ];
  } "touch $out";
}