Nix-installed binaries slow to start on Ubuntu

Hi folks,

I am using Nix on Ubuntu 24.04 and the binaries I’m installing via Nix (they happen to be installed via a flake) are all very slow the first time they are invoked (5+ seconds). It is not any particular binary, or at least it happens when any binary from my Nix-installed packages is run for the first time. The second and subsequent times any of them are run, they start quickly.

ubuntu@ip-172-31-9-246:~$ which terraform
/nix/var/nix/profiles/fxdevenv-systemwide/bin/terraform
ubuntu@ip-172-31-9-246:~$ time terraform --version
Terraform v1.10.5
on linux_amd64

Your version of Terraform is out of date! The latest version
is 1.11.3. You can update by downloading from https://www.terraform.io/downloads.html

real	0m14.864s
user	0m0.145s
sys	0m0.200s
ubuntu@ip-172-31-9-246:~$ time terraform --version
Terraform v1.10.5
on linux_amd64

Your version of Terraform is out of date! The latest version
is 1.11.3. You can update by downloading from https://www.terraform.io/downloads.html

real	0m0.066s
user	0m0.049s
sys	0m0.027s
ubuntu@ip-172-31-9-246:~$ which terramate
/nix/var/nix/profiles/fxdevenv-systemwide/bin/terramate
ubuntu@ip-172-31-9-246:~$ time terramate --version
0.11.9

real	0m7.079s
user	0m0.102s
sys	0m0.105s
ubuntu@ip-172-31-9-246:~$ time terramate --version
0.11.9

real	0m0.151s
user	0m0.082s
sys	0m0.053s
ubuntu@ip-172-31-9-246:~$ 

I’ve tried running strace against these executions but I see nothing in particular interesting, although that may be because I’m not sure what I’m looking for.

This is on an AWS-hosted Ubuntu VM.

This seems like normal behavior for cases of “cold start” delay with Nix-installed binaries on Ubuntu. Delay is usually caused by dynamic linker (“ld”) resolving dependencies outside the standard path.

I have read that you can create a shell wrapper like

export LD_PRELOAD=/nix/store/.../lib/libc.so:/nix/store/.../lib/libstdc++.so

There is also the possibility to use makeWrapper in a package, or override an existing package

{ pkgs ? import <nixpkgs> {} }:
pkgs.hello.overrideAttrs (old: {
  nativeBuildInputs = (old.nativeBuildInputs or []) ++ [ pkgs.makeWrapper ];
  postInstall = ''
    wrapProgram $out/bin/hello \
      --set LD_LIBRARY_PATH "${pkgs.lib.makeLibraryPath [ pkgs.glibc ]}"
  '';
})
1 Like