I’m extracting a shell script which on master is written inline as a string in a .nix file, moving it into a .sh file so that we can run tooling against it. Here’s what it looks like after my change:
fetch-deps = substituteAll {
src = ./fetchDeps.sh;
inherit pname defaultDepsFile runtimeIds storeSrc flags disableParallel
projectFileStr testProjectFileStr path dotnetSdkPath excludedSources;
};
Here, my fetchDeps.sh starts like this:
set -euo pipefail
export PATH="@path@"
This is all working exactly as I had hoped, except of course that the result of substituteAll does not have a hashbang at the top, so it’s not an executable.
What’s the idiomatic way to do this? Should I just define like bash = ''${pkgs.bash}/bin/bash'' and substitute that in with #!@bash@ (icky! would be nice if this template file either had no hashbang or a valid hashbang), or is there a cleaner way to do this?
NixOS:master ← Smaug123:fetchdeps-script
opened 09:11AM - 01 Jun 24 UTC
This makes the minimal change to put the script in a new file. It does not fix t… he resulting ShellCheck warnings (which would have been present in the previous version; I've just exposed them).
## Description of changes
So that we can run tooling (primarily ShellCheck) against the shell script, I've pulled it out into a new file.
The changes I've made to the Nix file are compliant with `nixfmt`.
Question: I haven't included a hashbang at the top of the file. How should I do that?
Testing: I took [a random one of my projects](https://github.com/Smaug123/WoofWare.Myriad/blob/04ecbe60023c528506ac26c134d52efc4e14a8ee/flake.nix#L48-L57). This is the diff that results between the two scripts on that project. Notice that the hashbang is gone. The resulting script correctly generates a lockfile. Lines with `<` are in the new version; lines with `>` are before this change.
```
0a1
> #!/nix/store/spyn2n8l9ca1pkqn20l40l424xmqwda4-bash-5.2p26/bin/bash
3c4
< export PATH="/nix/store/pldbvp0p2z2pa76h17g797vvk201155r-coreutils-9.5/bin:/nix/store/spyn2n8l9ca1pkqn20l40l424xmqwda4-bash-5.2p26/bin:/nix/store/nfsin5ji34p53977fpkn68zkavrxah7y-dotnet-sdk-8.0.301/bin:/nix/store/pwwk6vzi1jczh6hcbn6gc348148hyjlq-nuget-to-nix/bin"
---
> export PATH="/nix/store/pldbvp0p2z2pa76h17g797vvk201155r-coreutils-9.5/bin:/nix/store/spyn2n8l9ca1pkqn20l40l424xmqwda4-bash-5.2p26/bin:/nix/store/nfsin5ji34p53977fpkn68zkavrxah7y-dotnet-sdk-8.0.301/bin:/nix/store/kzjfv0cjdiyxbvfbnl33wlvjd8rj2azw-nuget-to-nix/bin"
73c74
< storeSrc="/nix/store/4cjkgi9kmkdfwp9xvgcyrc6ln6fzy90b-WoofWare.Myriad.Plugins-source"
---
> storeSrc="/nix/store/m9jnvfj6f8wvdq2mc8ghs66ip2bh1ydd-WoofWare.Myriad.Plugins-source"
81c82
< "/nix/store/nfsin5ji34p53977fpkn68zkavrxah7y-dotnet-sdk-8.0.301/bin/dotnet" tool restore
---
> /nix/store/nfsin5ji34p53977fpkn68zkavrxah7y-dotnet-sdk-8.0.301/bin/dotnet tool restore
84,86c85
< runtimeIds=('osx-arm64' 'linux-arm64' 'osx-x64' 'linux-x64')
<
< for rid in "${runtimeIds[@]}"; do
---
> for rid in "osx-arm64" "linux-arm64" "osx-x64" "linux-x64"; do
100c99
< excluded_sources=( '/nix/store/82z2qmr1lcds3vlh1ypa2dkhxmzxx10g-dotnet-sdk-8.0.301-deps-nuget-deps' )
---
> excluded_sources="/nix/store/82z2qmr1lcds3vlh1ypa2dkhxmzxx10g-dotnet-sdk-8.0.301-deps-nuget-deps"
```
## Things done
- Built on platform(s)
- [ ] x86_64-linux
- [ ] aarch64-linux
- [ ] x86_64-darwin
- [x] aarch64-darwin
- For non-Linux: Is sandboxing enabled in `nix.conf`? (See [Nix manual](https://nixos.org/manual/nix/stable/command-ref/conf-file.html))
- [x] `sandbox = relaxed`
- [ ] `sandbox = true`
- [ ] Tested, as applicable:
- [NixOS test(s)](https://nixos.org/manual/nixos/unstable/index.html#sec-nixos-tests) (look inside [nixos/tests](https://github.com/NixOS/nixpkgs/blob/master/nixos/tests))
- and/or [package tests](https://github.com/NixOS/nixpkgs/blob/master/pkgs/README.md#package-tests)
- or, for functions and "core" functionality, tests in [lib/tests](https://github.com/NixOS/nixpkgs/blob/master/lib/tests) or [pkgs/test](https://github.com/NixOS/nixpkgs/blob/master/pkgs/test)
- made sure NixOS tests are [linked](https://nixos.org/manual/nixpkgs/unstable/#ssec-nixos-tests-linking) to the relevant packages
- [ ] Tested compilation of all packages that depend on this change using `nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD"`. Note: all changes have to be committed, also see [nixpkgs-review usage](https://github.com/Mic92/nixpkgs-review#usage)
- [x] Tested basic functionality of all binary files (usually in `./result/bin/`)
- [24.11 Release Notes](https://github.com/NixOS/nixpkgs/blob/master/nixos/doc/manual/release-notes/rl-2411.section.md) (or backporting [23.11](https://github.com/NixOS/nixpkgs/blob/master/nixos/doc/manual/release-notes/rl-2311.section.md) and [24.05](https://github.com/NixOS/nixpkgs/blob/master/nixos/doc/manual/release-notes/rl-2405.section.md) Release notes)
- [ ] (Package updates) Added a release notes entry if the change is major or breaking
- [ ] (Module updates) Added a release notes entry if the change is significant
- [ ] (Module addition) Added a release notes entry if adding a new NixOS module
- [ ] Fits [CONTRIBUTING.md](https://github.com/NixOS/nixpkgs/blob/master/CONTRIBUTING.md).
<!--
To help with the large amounts of pull requests, we would appreciate your
reviews of other pull requests, especially simple package updates. Just leave a
comment describing what you have tested in the relevant package/service.
Reviewing helps to reduce the average time-to-merge for everyone.
Thanks a lot if you do!
List of open PRs: https://github.com/NixOS/nixpkgs/pulls
Reviewing guidelines: https://github.com/NixOS/nixpkgs/blob/master/pkgs/README.md#reviewing-contributions
-->
---
Add a :+1: [reaction] to [pull requests you find important].
[reaction]: https://github.blog/2016-03-10-add-reactions-to-pull-requests-issues-and-comments/
[pull requests you find important]: https://github.com/NixOS/nixpkgs/pulls?q=is%3Aopen+sort%3Areactions-%2B1-desc
Maybe use ${pkgs.runtimeShell} instead of ${pkgs.bash}/bin/bash, but yeah, I’ve seen substituteAll used like this.
1 Like
Don’t have the time to read the PR closely atm but I do wonder from the basics if this could be using writeShellApplication?
'';
checkPhase = ''
${stdenv.shellDryRun} "$target"
'';
meta.mainProgram = name;
};
# TODO: move parameter documentation to the Nixpkgs manual
# See doc/build-helpers/trivial-build-helpers.chapter.md
# or https://nixos.org/manual/nixpkgs/unstable/#trivial-builder-writeShellApplication
writeShellApplication =
{
/*
The name of the script to write.
Type: String
*/
name,
/*
The shell script's text, not including a shebang.
Oh amazing, I hand-rolled my own version of that elsewhere because I didn’t know it existed!
1 Like