|
| 1 | +# Nix build that defines a reproducible environment for running the |
| 2 | +# server. You can use this as an easy way to jump into the dev |
| 3 | +# environment without depending on any OS-level software packages. |
| 4 | +# |
| 5 | +# Requirements: nix must be installed; use "apt install nix-bin" or |
| 6 | +# see https://nixos.org/ for more detailed options. |
| 7 | +# |
| 8 | +# Usage: |
| 9 | +# |
| 10 | +# To jump into a dev shell: |
| 11 | +# nix-shell |
| 12 | +# |
| 13 | +# To create a permanent shell for later use: |
| 14 | +# nix-build -o devshell |
| 15 | +# |
| 16 | +# Updating: |
| 17 | +# When yarn.lock or package.json get updated, we need to build a new |
| 18 | +# "offline cache" and recrod its hash. Nix will raise an error in |
| 19 | +# this case; please update the hash, try nix-shell again, and make a |
| 20 | +# PR if it still works. |
| 21 | +# |
| 22 | +# Note: this includes a dependency on a personal GitHub repository for |
| 23 | +# the simple mkBuildableShell utility. If that URL ever breaks we |
| 24 | +# could replace it with the less-functional mkShell or copy in the |
| 25 | +# code from a backup somewhere. |
| 26 | + |
| 27 | +let |
| 28 | + newYarnLockHash = (builtins.hashFile "md5" yarnLock); |
| 29 | + offlineCache = |
| 30 | + pkgs.fetchYarnDeps { |
| 31 | + inherit yarnLock; |
| 32 | + name = "online-go-offline-cache-#{newYarnLockHash}"; |
| 33 | + sha256 = "nSoatjgLdCrlgmdKsJIA3YqU1fNII+06XgHMEwePZEg="; |
| 34 | + }; |
| 35 | + |
| 36 | + # nixos-25.11 from 2026-03-15: |
| 37 | + nixpkgs = |
| 38 | + let version = "8fd9daa3db09ced9700431c5b7ad0e8ba199b575"; |
| 39 | + in fetchTarball { |
| 40 | + name = "nixpkgs-${version}"; |
| 41 | + url = "https://github.com/NixOS/nixpkgs/archive/${version}.tar.gz"; |
| 42 | + sha256 = "1i4bkzy1siavmxaskp49lgi9s02gam6crb0d0abbbjmsyl39jbsf"; |
| 43 | + }; |
| 44 | + pkgs = (import nixpkgs {}); |
| 45 | + |
| 46 | + # mkBuildableShell from 2026-04-11: |
| 47 | + mkBuildableShell-src = |
| 48 | + let version = "064d5a1bba6236314a14421fadb873195854b0b5"; |
| 49 | + in fetchTarball { |
| 50 | + name = "mkBuildableShell-${version}"; |
| 51 | + url = "https://github.com/pdg137/mkBuildableShell/archive/${version}.tar.gz"; |
| 52 | + sha256 = "0dsk7anb6c5f9bd72dkzzzwi4mazdji25h4m9wfn2xfvkixns10i"; |
| 53 | + }; |
| 54 | + mkBuildableShell = (import mkBuildableShell-src pkgs); |
| 55 | + |
| 56 | + yarnLock = ./yarn.lock; |
| 57 | + packageJson = ./package.json; |
| 58 | + node = pkgs.nodejs_24; |
| 59 | + |
| 60 | + # This program "napi-postinstall" didn't work as expected fom a |
| 61 | + # post-install script of unrs-resolver, maybe because of |
| 62 | + # /usr/bin/env or because of just not being on the path correctly. |
| 63 | + # So we make an alias to fix it: |
| 64 | + napi-postinstall-alias = pkgs.writeShellScriptBin "napi-postinstall" |
| 65 | + ''exec ${node}/bin/node ../napi-postinstall/lib/cli.js "$@"''; |
| 66 | + |
| 67 | + node-modules = pkgs.stdenv.mkDerivation { |
| 68 | + name = "ogs-node-modules"; |
| 69 | + |
| 70 | + dontUnpack = true; |
| 71 | + |
| 72 | + nativeBuildInputs = [ |
| 73 | + pkgs.yarn |
| 74 | + pkgs.fixup-yarn-lock |
| 75 | + napi-postinstall-alias |
| 76 | + ]; |
| 77 | + |
| 78 | + buildPhase = '' |
| 79 | + set -e |
| 80 | +
|
| 81 | + # Use local setup for Yarn with internet access disabled. |
| 82 | + export HOME="$(mktemp -d)" |
| 83 | + yarn config --offline set yarn-offline-mirror ${offlineCache} |
| 84 | +
|
| 85 | + cp ${yarnLock} ./yarn.lock |
| 86 | + cp ${packageJson} ./package.json |
| 87 | + chmod u+w ./yarn.lock |
| 88 | + fixup-yarn-lock yarn.lock |
| 89 | +
|
| 90 | + yarn install --offline --frozen-lockfile \ |
| 91 | + --no-progress --non-interactive |
| 92 | + ''; |
| 93 | + |
| 94 | + # Note: rollup seems to fail if the directory is not called |
| 95 | + # "node_modules", even behind a symlink. |
| 96 | + installPhase = '' |
| 97 | + set -eu |
| 98 | + mkdir -p $out |
| 99 | + mv node_modules $out |
| 100 | + ''; |
| 101 | + }; |
| 102 | + |
| 103 | +in |
| 104 | +mkBuildableShell { |
| 105 | + name = "shell"; |
| 106 | + buildInputs = [ node pkgs.yarn ]; |
| 107 | + |
| 108 | + shellHook = '' |
| 109 | + set -eu |
| 110 | +
|
| 111 | + if [ -e node_modules ] && [ ! -L node_modules ]; then |
| 112 | + echo 'Existing ./node_modules directory found; remove before proceeding.' |
| 113 | + exit 1 |
| 114 | + else |
| 115 | + echo 'Linking node_modules to Nix store...' |
| 116 | + rm -f ./node_modules |
| 117 | + ln -sf ${node-modules}/node_modules ./node_modules |
| 118 | + fi |
| 119 | + export OGS_LOCAL_VITE_CACHE=1 |
| 120 | +
|
| 121 | + echo 'Run "yarn run dev" to start the server.' |
| 122 | + set +eu |
| 123 | + ''; |
| 124 | +} |
0 commit comments