|
| 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 | +# If yarn.lock or package.json get updated, the build will fail due |
| 18 | +# to missing packages or a mismatched hash in the defnition of |
| 19 | +# offlineCache below. You can force it to display the new hash by |
| 20 | +# blanking out the existing one, then update the hash to the new |
| 21 | +# value to proceed. (Please submit a PR.) |
| 22 | +# |
| 23 | +# Note: this includes a dependency on a personal GitHub repository for |
| 24 | +# the simple mkBuildableShell utility. If that URL ever breaks we |
| 25 | +# could replace it with the less-functional mkShell or copy in the |
| 26 | +# code from a backup somewhere. |
| 27 | + |
| 28 | +let |
| 29 | + offlineCache = pkgs.fetchYarnDeps { |
| 30 | + inherit yarnLock; |
| 31 | + # Update this hash for a new yarn.lock. |
| 32 | + sha256 = "GyAf2MY4jex4rJakGXhbufOPNCWoX6u6zaByFfF8n2A="; |
| 33 | + }; |
| 34 | + |
| 35 | + # nixos-25.11 from 2026-03-05: |
| 36 | + nixpkgs = |
| 37 | + let version = "fabb8c9deee281e50b1065002c9828f2cf7b2239"; |
| 38 | + in fetchTarball { |
| 39 | + name = "nixpkgs-${version}"; |
| 40 | + url = "https://github.com/NixOS/nixpkgs/archive/${version}.tar.gz"; |
| 41 | + sha256 = "15gvdgdqsxjjihq1r66qz1q97mlcaq1jbpkhbx287r5py2vy38b1"; |
| 42 | + }; |
| 43 | + pkgs = (import nixpkgs {}); |
| 44 | + |
| 45 | + # mkBuildableShell from 2026-04-11: |
| 46 | + mkBuildableShell-src = |
| 47 | + let version = "064d5a1bba6236314a14421fadb873195854b0b5"; |
| 48 | + in fetchTarball { |
| 49 | + name = "mkBuildableShell-${version}"; |
| 50 | + url = "https://github.com/pdg137/mkBuildableShell/archive/${version}.tar.gz"; |
| 51 | + sha256 = "0dsk7anb6c5f9bd72dkzzzwi4mazdji25h4m9wfn2xfvkixns10i"; |
| 52 | + }; |
| 53 | + mkBuildableShell = (import mkBuildableShell-src pkgs); |
| 54 | + |
| 55 | + yarnLock = ./yarn.lock; |
| 56 | + packageJson = ./package.json; |
| 57 | + node = pkgs.nodejs_24; |
| 58 | + |
| 59 | + # This program "napi-postinstall" didn't work as expected fom a |
| 60 | + # post-install script of unrs-resolver, maybe because of |
| 61 | + # /usr/bin/env or because of just not being on the path correctly. |
| 62 | + # So we make an alias to fix it: |
| 63 | + napi-postinstall-alias = pkgs.writeShellScriptBin "napi-postinstall" |
| 64 | + ''exec ${node}/bin/node ../napi-postinstall/lib/cli.js "$@"''; |
| 65 | + |
| 66 | + node-modules = pkgs.stdenv.mkDerivation { |
| 67 | + name = "ogs-node-modules"; |
| 68 | + |
| 69 | + dontUnpack = true; |
| 70 | + |
| 71 | + nativeBuildInputs = [ |
| 72 | + pkgs.yarn |
| 73 | + pkgs.fixup-yarn-lock |
| 74 | + napi-postinstall-alias |
| 75 | + ]; |
| 76 | + |
| 77 | + buildPhase = '' |
| 78 | + set -e |
| 79 | +
|
| 80 | + # Use local setup for Yarn with internet access disabled. |
| 81 | + export HOME="$(mktemp -d)" |
| 82 | + yarn config --offline set yarn-offline-mirror ${offlineCache} |
| 83 | +
|
| 84 | + cp ${yarnLock} ./yarn.lock |
| 85 | + cp ${packageJson} ./package.json |
| 86 | + chmod u+w ./yarn.lock |
| 87 | + fixup-yarn-lock yarn.lock |
| 88 | +
|
| 89 | + yarn install --offline --frozen-lockfile \ |
| 90 | + --no-progress --non-interactive |
| 91 | + ''; |
| 92 | + |
| 93 | + # Note: rollup seems to fail if the directory is not called |
| 94 | + # "node_modules", even behind a symlink. |
| 95 | + installPhase = '' |
| 96 | + set -eu |
| 97 | + mkdir -p $out |
| 98 | + mv node_modules $out |
| 99 | + ''; |
| 100 | + }; |
| 101 | + |
| 102 | +in |
| 103 | +mkBuildableShell { |
| 104 | + name = "shell"; |
| 105 | + buildInputs = [ node pkgs.yarn ]; |
| 106 | + |
| 107 | + shellHook = '' |
| 108 | + set -eu |
| 109 | +
|
| 110 | + if [ -e node_modules ] && [ ! -L node_modules ]; then |
| 111 | + echo 'Existing ./node_modules directory found; remove before proceeding.' |
| 112 | + exit 1 |
| 113 | + else |
| 114 | + echo 'Linking node_modules to Nix store...' |
| 115 | + rm -f ./node_modules |
| 116 | + ln -sf ${node-modules}/node_modules ./node_modules |
| 117 | + fi |
| 118 | + export OGS_LOCAL_VITE_CACHE=1 |
| 119 | +
|
| 120 | + echo 'Run "yarn run dev" to start the server.' |
| 121 | + set +eu |
| 122 | + ''; |
| 123 | +} |
0 commit comments