Help/review finishing PR to fix building Coder

Past day or so I’ve worked on getting Coder building on my M1 mac and finally got it building. (Branch on my fork)

However, when I went back to test on linux builds I got strange errors

❯ nix build .#packages.x86_64-linux.coder
warning: Git tree '/Users/cmp/src/infra' is dirty
error: build of '/nix/store/d0yqvc27iz2nh3mpwp17ldjxdkxs6xa3-coder-2.0.2.drv' on 'ssh://cmp@nix.gorgon-basilisk.ts.net' failed: builder for '/nix/store/d0yqvc27iz2nh3mpwp17ldjxdkxs6xa3-coder-2.0.2.drv' failed with exit code 1;
       last 10 log lines:
       >  5: 0xe9d0d5  [node]
       >  6: 0xeb1519 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
       >  7: 0xe8dcaa v8::internal::HeapAllocator::AllocateRawWithLightRetrySlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node]
       >  8: 0xe8f054 v8::internal::HeapAllocator::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node]
       >  9: 0xe6f75d v8::internal::Factory::NewFillerObject(int, v8::internal::AllocationAlignment, v8::internal::AllocationType, v8::internal::AllocationOrigin) [node]
       > 10: 0x1257a5b v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [node]
       > 11: 0x16a2179  [node]
       >  ERR_PNPM_RECURSIVE_EXEC_FIRST_FAIL  Command was killed with SIGABRT (Aborted): vite build
       >  ELIFECYCLE  Command failed with exit code 1.
       > /nix/store/fzb9wy1yz0hn69vxw12954szvrjnjjgk-stdenv-linux/setup: line 144: pop_var_context: head of shell_variables not a function context
       For full logs, run 'nix log /nix/store/d0yqvc27iz2nh3mpwp17ldjxdkxs6xa3-coder-2.0.2.drv'.
error: builder for '/nix/store/d0yqvc27iz2nh3mpwp17ldjxdkxs6xa3-coder-2.0.2.drv' failed with exit code 1;
       last 10 log lines:
       >  5: 0xe9d0d5  [node]
       >  6: 0xeb1519 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
       >  7: 0xe8dcaa v8::internal::HeapAllocator::AllocateRawWithLightRetrySlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node]
       >  8: 0xe8f054 v8::internal::HeapAllocator::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node]
       >  9: 0xe6f75d v8::internal::Factory::NewFillerObject(int, v8::internal::AllocationAlignment, v8::internal::AllocationType, v8::internal::AllocationOrigin) [node]
       > 10: 0x1257a5b v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [node]
       > 11: 0x16a2179  [node]
       >  ERR_PNPM_RECURSIVE_EXEC_FIRST_FAIL  Command was killed with SIGABRT (Aborted): vite build
       >  ELIFECYCLE  Command failed with exit code 1.
       > /nix/store/fzb9wy1yz0hn69vxw12954szvrjnjjgk-stdenv-linux/setup: line 144: pop_var_context: head of shell_variables not a function context
       For full logs, run 'nix log /nix/store/d0yqvc27iz2nh3mpwp17ldjxdkxs6xa3-coder-2.0.2.drv'.

I’ve been working off a private flake I have with all the same changes as the package changes in my fork.

Was hoping to see if someone could replicate the issue on their side or point out anything obviously wrong.

It works fine on aarch64-darwin and x86_64-darwin but fails on both aarch64-linux and x86_64-linux on two separate systems (both VMs). The issue is clearly within the pnpm build line of the preBuild, but what exactly is causing the issue is unclear to me.

In theory, this should work because the Coder project has built a binary for Linux and it also works when using Darwin for me. I don’t think there is something specific to my environment messing with this, but would appreciate help making sure.

Fixed the issues on Linux by giving Node more heap space. Something which seems obvious in hindsight looking at this error, but why it only happened on Linux kind of stumps me.

PR created (all green) and hopefully this makes it possible to run coder directly on NixOS again instead of in a docker container or VM.

Discovered an issue when actually using the coder server for real which requires building all the versions of the slim binary as well. I think it’s pretty doable, but requires some rethinking of the changes I made.

For the meantime I’m using this in my private repo

{ lib
, fetchurl
, installShellFiles
, makeWrapper
, terraform
, stdenvNoCC
, unzip
}:
let
  inherit (stdenvNoCC.hostPlatform) system;
in

stdenvNoCC.mkDerivation rec {
  pname = "coder";
  version = "2.1.5";

  src = fetchurl {
    sha256 = {
      x86_64-linux = "sha256-yIhyVxsYfMfsNvMu6wohs/57FXYCnPSibR8l3eu1p3M=";
      x86_64-darwin = lib.fakeHash;
      aarch64-linux = lib.fakeHash;
      aarch64-darwin = "sha256-yDIns1iZ2wEpXBQLoo/krchv+ETL3rpmFbDvAu1VkXA=";
    }.${system};

    url =
      let
        systemName = {
          x86_64-linux = "linux_amd64";
          aarch64-linux = "linux_arm64";
          x86_64-darwin = "darwin_amd64";
          aarch64-darwin = "darwin_arm64";
        }.${system};

        ext = {
          x86_64-linux = "tar.gz";
          aarch64-linux = "tar.gz";
          x86_64-darwin = "zip";
          aarch64-darwin = "zip";
        }.${system};
      in
      "https://github.com/coder/coder/releases/download/v${version}/coder_${version}_${systemName}.${ext}";
  };

  nativeBuildInputs = [
    installShellFiles
    makeWrapper
    unzip
  ];

  unpackPhase = ''
    printf 'Decompressing %s\n' "$src"
    case $src in
        *.tar.gz) tar -xz -f "$src" ;;
        *.zip)    unzip      "$src" ;;
    esac
  '';

  installPhase = ''
    mkdir -p $out/bin
    cp coder $out/bin
  '';

  postInstall = ''
    installShellCompletion --cmd coder \
      --bash <($out/bin/coder completion bash) \
      --fish <($out/bin/coder completion fish) \
      --zsh <($out/bin/coder completion zsh)

    wrapProgram $out/bin/coder --prefix PATH : ${lib.makeBinPath [ terraform ]}
  '';

  # integration tests require network access
  doCheck = false;

  meta = {
    description = "Provision software development environments via Terraform on Linux, macOS, Windows, X86, ARM, and of course, Kubernetes";
    homepage = "https://coder.com";
    license = lib.licenses.agpl3;
    maintainers = [ lib.maintainers.ghuntley lib.maintainers.urandom ];
    broken = false;
  };
}

Works perfectly with the existing nixos coder service.