I am attempting to move a bash script to its own file using the setup hook makeWrapper
along with the readFile
, simlinkJoin
and writeShellScriptBin
functions. but for some reason it doesn’t seem to find any files. What I’ve tried so far is to read the script, writing it to the store and making it executable. Here is the full flake and here are the relevant bits:
nanolibs-script = rec {
name = "nanolibs-path";
source = builtins.readFile ./nanolibs-script.sh;
script = (pkgs.writeShellScriptBin name source).overrideAttrs(old: {
buildCommand = "${old.buildCommand}\n patchShebangs $out";
});
buildInputs = [
riscv-toolchain.newlib-nano
];
};
I then create a new derivation with symlinkJoin
by adding symlinks to each of the paths, that is, the script wrapper and the libraries, then making it executable withmakeWrapper
:
packages = {
nanolibsPath = pkgs.symlinkJoin {
name = "nanolibs-path";
paths = [ nanolibs-script.script ] ++ nanolibs-script.buildInputs;
buildInputs = with pkgs; [
makeWrapper
];
postBuild = ''
wrapProgram $out/bin/${nanolibs-script.name} --prefix PATH : $out/bin
'';
};
Finally, I create a devShell
with a shellHook
that runs the executable:
devShells = {
fe310Shell = pkgs.mkShell {
buildInputs = with pkgs; [
riscv-toolchain.buildPackages.gcc
openocd
];
shellHook = ''
nix run .#nanolibsPath
'';
};
The script itself is located in the same directory as the flake, it’s contents being:
#!/usr/bin/env bash
rm -fr nanolibs/*.a
mkdir -p nanolibs
for file in "${riscv-toolchain.newlib-nano}"/riscv32-none-elf/lib/*.a; do
ln -s "$file" nanolibs
done
for file in nanolibs/*.a; do
mv "$file" "${file%%.a}_nano.a"
done
As explained at the beginning, instead of all archive files in ${riscv-toolchain.newlib-nano}"/riscv32-none-elf/lib/
, the above creates the nanolibs
directory and renames a ghost file *_nano.a
. It does not seem to find the files. Not an issue with the bash script since having this script inline, in the flake itself, does what is expected of it, such as in the following snippet:
nanolibs-script = rec {
name = "nanolibs-path";
source = ''
rm -fr nanolibs/*.a
mkdir -p nanolibs
for file in "${riscv-toolchain.newlib-nano}"/riscv32-none-elf/lib/*.a; do
ln -s "$file" nanolibs
done
for file in nanolibs/*.a; do
mv "$file" "''${file%%.a}_nano.a"
done
'';
buildInputs = [
riscv-toolchain.newlib-nano
];
};
Then on my devShell, I replace the nix run command
with shellHook = nanolibs-script.script;
and it does what is expected, it renames all archive files in the ${riscv-toolchain.newlib-nano}"/riscv32-none-elf/lib
directory and symlinks them into the nanolibs
directory.