nixnixosnix-flakeflakedevbox

Cannot run nix flake on MacBook M1


I am using the following flake to setup bun@1.1.13 since that version is not yet available in Nix Store.

{
  description = "Runtimes required by Platform";

  inputs = {
    nixpkgs.url = "nixpkgs/nixos-unstable";
  };

  outputs = { self, nixpkgs }: {
    packages = let
      bunVersion = "1.1.13";

      getBun = { system, sha256 }: let
        pkgs = import nixpkgs { inherit system; };
        bunSrc = pkgs.fetchurl {
          url = "https://github.com/oven-sh/bun/releases/download/bun-v${bunVersion}/bun-${system}.zip";
          inherit sha256;
        };

      in pkgs.stdenv.mkDerivation {
        pname = "bun";
        version = bunVersion;

        src = bunSrc;

        nativeBuildInputs = [ pkgs.unzip ];

        installPhase = ''
          mkdir -p $out/bin
          unzip $src -d $out/bin
          chmod +x $out/bin/bun
        '';

        meta = with pkgs.lib; {
          description = "Bun - JavaScript runtime";
          homepage = "https://bun.sh/";
          license = licenses.mit;
          platforms = platforms.all;
        };
      };

      bun_darwin_aarch64 = getBun { system = "darwin-aarch64"; sha256 = "144bvn34v26shi50ykrfvwm439qnqn7z2v903dxm0wzbwqpsf3m6"; };

      bun_darwin_x64 = getBun { system = "darwin-x64"; sha256 = "1h6p9sb9dyx3i5dw3xfm20np6wh0p0blm2204fpig4dph5chdrim"; };
      
      bun_linux_aarch64 = getBun { system = "linux-x64"; sha256 = "1xwq9n7gpbpz8dyvcpavlr9i4lrl7kxkxwc84a0jnqnid2qrsbj0"; };

    in {
      aarch64-darwin.bun = bun_darwin_aarch64;
      x86_64-darwin.bun = bun_darwin_x64;
      x86_64-linux.bun = bun_linux_aarch64;
    };
  };
}

I am using MacBook M1 and was expecting bun@1.1.13 for darwin-aarch64 properly installed.

However, I am getting error:

Error: error installing package path:flakes/runtime#bun
source: nix: command error: nix --extra-experimental-features ca-derivations --option experimental-features 'nix-command flakes fetch-closure' path-info 'path:/Users/mp/workspace/com/proj/flakes/runtime#bun' --json --impure: Unknown CPU type: darwin: exit code 1

Solution

  • There are some issues with your flake:

    wrong system

    The first problem with your flake is that you are trying to specify "linux-64" (or "darwin-64", ...) as system there:

     getBun = { system, sha256 }: let
            pkgs = import nixpkgs { inherit system; };
                                            ^
    

    It is not a valid system name in nix. That issue is coming from bun's releases using strange system names themself. The correct sollution would therefore be to separate system that is going to be used for pkgs and packageSystemName that is used in package archive url. Now getBun looks like:

    getBun = { system, packageSystemName, sha256 }: let
            pkgs = import nixpkgs { inherit system; };
            bunSrc = pkgs.fetchurl {
              url = "https://github.com/oven-sh/bun/releases/download/bun-v${bunVersion}/bun-${packageSystemName}.zip";
              inherit sha256;
            };
    
          in pkgs.stdenv.mkDerivation {....}
    

    And its usage:

    bun_darwin_aarch64 = getBun { system = "aarch64-darwin"; packageSystemName = "darwin-aarch64"; sha256 = "144bvn34v26shi50ykrfvwm439qnqn7z2v903dxm0wzbwqpsf3m6"; };
    
    bun_darwin_x64 = getBun { system = "x86_64-darwin"; packageSystemName = "darwin-x64"; sha256 = "1h6p9sb9dyx3i5dw3xfm20np6wh0p0blm2204fpig4dph5chdrim"; };
    
    bun_linux_aarch64 = getBun { system = "x86_64-linux"; packageSystemName = "linux-x64"; sha256 = "1xwq9n7gpbpz8dyvcpavlr9i4lrl7kxkxwc84a0jnqnid2qrsbj0"; };
    

    wrong mkdir

    Because you do not mkdir $out/bin/bun after fixing first issue you will encounter:

    chmod: cannot access '/nix/store/h06cjf6z577v2vfhsg6dl4ywjckbfcvq-bun-1.1.13/bin/bun'
    

    Just change your mkdir line in installPhase to:

    mkdir -p $out/bin/bun
    

    Result

    Now flake looks like:

    {
      description = "Runtimes required by Platform";
    
      inputs = {
        nixpkgs.url = "nixpkgs/nixos-unstable";
      };
    
      outputs = { self, nixpkgs }: {
        packages = let
          bunVersion = "1.1.13";
    
          getBun = { system, packageSystemName, sha256 }: let
            pkgs = import nixpkgs { inherit system; };
            bunSrc = pkgs.fetchurl {
              url = "https://github.com/oven-sh/bun/releases/download/bun-v${bunVersion}/bun-${packageSystemName}.zip";
              inherit sha256;
            };
    
          in pkgs.stdenv.mkDerivation {
            pname = "bun";
            version = bunVersion;
    
            src = bunSrc;
    
            nativeBuildInputs = [ pkgs.unzip ];
    
            installPhase = ''
              mkdir -p $out/bin/bun
              unzip $src -d $out/bin
              chmod +x $out/bin/bun
            '';
    
            meta = with pkgs.lib; {
              description = "Bun - JavaScript runtime";
              homepage = "https://bun.sh/";
              license = licenses.mit;
              platforms = platforms.all;
            };
          };
    
          bun_darwin_aarch64 = getBun { system = "aarch64-darwin"; packageSystemName = "darwin-aarch64"; sha256 = "144bvn34v26shi50ykrfvwm439qnqn7z2v903dxm0wzbwqpsf3m6"; };
    
          bun_darwin_x64 = getBun { system = "x86_64-darwin"; packageSystemName = "darwin-x64"; sha256 = "1h6p9sb9dyx3i5dw3xfm20np6wh0p0blm2204fpig4dph5chdrim"; };
    
          bun_linux_aarch64 = getBun { system = "x86_64-linux"; packageSystemName = "linux-x64"; sha256 = "1xwq9n7gpbpz8dyvcpavlr9i4lrl7kxkxwc84a0jnqnid2qrsbj0"; };
    
        in {
          aarch64-darwin.bun = bun_darwin_aarch64;
          x86_64-darwin.bun = bun_darwin_x64;
          x86_64-linux.bun = bun_linux_aarch64;
        };
      };
    }
    

    And it works:

    nix build .\#packages.x86_64-linux.bun
    file result/bin/bun-linux-x64/bun
    result/bin/bun-linux-x64/bun: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[xxHash]=294ede76aad1b16f, stripped