/ flake.nix
flake.nix
  1  {
  2    inputs = {
  3      flake-utils.url = "github:numtide/flake-utils";
  4      fedimint.url =
  5        "github:fedimint/fedimint?rev=e147b5695abce6eeeb984974d48d562b2bf1dfe1";
  6    };
  7    outputs = { self, flake-utils, fedimint }:
  8      flake-utils.lib.eachDefaultSystem (system:
  9        let
 10          nixpkgs = fedimint.inputs.nixpkgs;
 11          pkgs = import nixpkgs {
 12            inherit system;
 13            overlays = [ fedimint.overlays.all ];
 14          };
 15          fmLib = fedimint.lib.${system};
 16  
 17  
 18          # When `yarn.lock` changes, follow these steps to update the `sha256` hash:
 19          # 1. Remove the existing `sha256` hash.
 20          # 2. Rebuild the Nix derivation by executing:
 21          #    nix build .#guardian-ui
 22          # 3. Obtain the new `sha256` hash from the build error message.
 23          # 4. Update the `sha256` value below with the newly copied hash.
 24          #
 25          # **Important:** 
 26          # Keeping the `sha256` hash in sync with `yarn.lock` is essential. An outdated or incorrect hash 
 27          # will cause the Nix package to become out-of-sync with Yarn dependencies, potentially leading 
 28          # to build failures or inconsistent behavior.
 29          yarnOfflineCache = pkgs.fetchYarnDeps {
 30            yarnLock = ./yarn.lock;
 31            hash = "sha256-a1YeZYvxstAqDCgK2qrlylkKYhIX1Xo28tCKt46hxrE=";
 32          };
 33        in
 34        {
 35          devShells = fmLib.devShells // {
 36            default = fmLib.devShells.default.overrideAttrs (prev: {
 37              nativeBuildInputs = [
 38                pkgs.mprocs
 39                pkgs.nodejs
 40                pkgs.yarn
 41                fedimint.packages.${system}.devimint
 42                fedimint.packages.${system}.gateway-pkgs
 43                fedimint.packages.${system}.fedimint-pkgs
 44              ] ++ prev.nativeBuildInputs;
 45              shellHook = ''
 46                yarn install
 47              '';
 48            });
 49          };
 50  
 51          packages.guardian-ui = pkgs.stdenv.mkDerivation {
 52            pname = "guardian-ui";
 53            version = "0.4.3";
 54            src = ./.;
 55  
 56            nativeBuildInputs = with pkgs; [
 57              nodejs
 58              yarn
 59              cacert
 60              yarn2nix-moretea.fixup_yarn_lock
 61              nodePackages.serve
 62            ];
 63  
 64            configurePhase = ''
 65              export HOME=$(mktemp -d)
 66            '';
 67  
 68            # NixOS is introducing `yarnBuildHook`, `yarnConfigHook`, and `yarnInstallHook`,
 69            # which could simplify the current complex `buildPhase` that uses `fixup_yarn_lock`
 70            # and the `installPhase`.
 71            # 1. [JavaScript Frameworks Documentation](https://github.com/NixOS/nixpkgs/blob/ab4dc6ca78809a367aba6fb2813a15116560e2a9/doc/languages-frameworks/javascript.section.md)
 72            # 2. [Nixpkgs Issue #324246](https://github.com/NixOS/nixpkgs/issues/324246)
 73            #
 74            # Additionally, avoid using `pkgs.mkYarnPackage` as it is slated for deprecation.
 75  
 76            buildPhase = ''
 77              yarn config --offline set yarn-offline-mirror ${yarnOfflineCache}
 78              fixup_yarn_lock yarn.lock
 79  
 80              yarn install --offline \
 81                --frozen-lockfile \
 82                --ignore-engines --ignore-scripts
 83              patchShebangs .
 84  
 85              yarn build
 86            '';
 87  
 88            # should be similar to the installer in the Dockerfile
 89            installPhase = ''
 90              mkdir -p $out
 91              # static files that can be served by reverse-proxies
 92              cp -r apps/router/build/* $out
 93  
 94              # wrapper for "nix run .#guardian-ui" (based on https://wiki.nixos.org/wiki/Node.js#Packaging_with_yarn2nix)
 95              mkdir -p $out/bin
 96              echo "#!/bin/sh" > $out/bin/guardian-ui
 97              echo "exec ${pkgs.nodePackages.serve}/bin/serve -s $out" >> $out/bin/guardian-ui
 98              chmod +x $out/bin/guardian-ui
 99            '';
100          };
101        });
102  }