@7c6f434c
I tried to do what you mean with the latest commit here: https://github.com/NixOS/nixpkgs/pull/175160
But I don’t think it is possible to achieve what you want with the current code. Let me explain:
I use impure nix functions at evaluation time. This leads to the following sequence:
- Get/Precompute the latest libreoffice version in nix and write it to update.sh file with
writeScript
function (impure/side-effect at eval time)
- Run the update.sh file to update
If you want the side effect to happen during (== runtime) the update.sh file, then you must write the impure code in bash. So step 1 is not needed and therefore we can just write the complete update script in bash.
So why do I want this impurity in step 1? It isolates the side effect to eval-time (hard cut). If everything works, then I gain nothing. But if the side-effect (e.g. can’t download version/sha256) fails I immediately stop with a custom error message and don’t run the update.sh file.
We also have other benefits of isolating the side-effect:
- We can use nix.
- Even if you design your updateScript poorly, you will always isolate the side-effect to eval time, no matter how you write your code. Therefore you automatically get these benefits listed here if you use that approach.
- We can test it easily. If we want to be extra safe we can even build the updateScript first with
nix-build -A libreoffice-bin.updateScript
and look at the update.sh file if it really contains the latest version instead of garbage.
We could of course do the same in bash and isolate the side-effect/download of version to a single bash-function and stop execution if it fails. This has the following disadvantages in my opinion:
- Often if the bash script fails, the real error is swallowed by the
update-source-version
or other scripts and remains hidden.
- We don’t get automatic isolation of the side-effect, you need to encapsulate into a function yourself.
- The update script usually needs the following info: url, sha256, platform strings (possibly mapped) etc. All this info needs to be passed from the derivation (nix) or either duplicated.
In general, bash is a good approach and despite my comparison here, my main point is not nix is better than bash but the following: It is ok if you use impure nix at runtime too.
The only disadvantage of the nix approach is its impurity. As a result, you can only run it with the --impure flag: nix build .libreoffice-bin.passthru.updateScript --impure
. Does this currently have any impact on nixpkgs?
No because the passthru
attributes are removed before the stdenv.mkDerivation
call and therefore the nixpkgs derivation remains pure. That is the nice purpose of the passthru
attributes.
Only if you want to introduce a pure eval check of the passthru.updateScript
attribute then it will fail.
But it will fail by design, because in my opinion the updateScript
is not like any other derivation: It is an impure derivation by design.
To sum it up: It is ok to write your updateScript in impure nix at runtime.
Edited because it is possible to run the side effect at runtime. See post below.