Opam init is failing for rescript-compiler wth nix

I’m trying to write mkDerivation for rescript-compiler. I’m getting this error

[ERROR] Could not write /homeless-shelter/.opam/opam-init/hooks/sandbox.sh
       > Fatal error:
       > /nix/store/i5yy77f6plfbcx8asg7h000n7w7wfi45-opam-2.1.5/bin/.opam-wrapped: "chmod" failed on /homeless-shelter/.opam/opam-init/hooks/sandbox.sh: No such file or directory

while running opam init -a.

builder.sh

mkdir $out
cd $out
opam init -a

flake.nix

{
    description = "Basic flake with mkDerivation";
    inputs = {
        nixpkgs = {
            url = "github:NixOS/nixpkgs";
        };
        flake-parts = { url = "github:hercules-ci/flake-parts"; inputs.nixpkgs-lib.follows = "nixpkgs"; };
        flake-utils = { url = "github:numtide/flake-utils"; };
    };
    outputs = {self, nixpkgs, flake-utils, ...}:
        flake-utils.lib.eachDefaultSystem (system: let
            pkgs = nixpkgs.legacyPackages.${system};
            in
            {   packages.default = pkgs.stdenv.mkDerivation
                    {
                        name = "main";
                        builder = "${pkgs.bash}/bin/bash";
                        args = [ ./builder.sh ];
                        inherit (pkgs) coreutils;
                        src = ./.;
                        system = builtins.currentSystem;
                        PATH = "${pkgs.diffutils}/bin:${pkgs.gnutar}/bin:${pkgs.patch}/bin:${pkgs.coreutils}/bin:${pkgs.mercurial}/bin:${pkgs.darcs}/bin:${pkgs.git}/bin:${pkgs.rsync}/bin:${pkgs.ocamlformat}/bin:${pkgs.re2c}/bin:${pkgs.python3}/bin:${pkgs.opam}/bin:${pkgs.gcc}/bin:${pkgs.clang}/bin:${pkgs.gnumake}/bin:${pkgs.nodejs}/bin";
                    };
            });
}

But opam init is working, when running from terminal instead of builder.sh.

1 Like

$HOME is set to /homeless-shelter within the build sandbox.

If your tooling requires an actual writable home it is common to do something like this in preConfigure:

export HOME=$(pwd)/build-home
mkdir -p $HOME
1 Like