Add shebang trick to documentation

This commit is contained in:
Tim Van Baak 2025-02-24 13:04:10 -08:00
parent 27eb3cb211
commit e242095aba

View File

@ -180,3 +180,20 @@ You can generalize this using `jq`'s `env` and set the message as a source envir
# with MESSAGE defined on the source
sh -c "cat /dev/random | base32 | head -c8 | jq -cR '{id: ., title: env.MESSAGE}'"
```
For more complicated logic, writing an update script in the data directory is usually necessary.
These can be written in any language.
For languages like Python where you might want dependencies, a useful trick is to embed the setup the setup in the update script as a polyglot with bash:
```python
#!/usr/bin/env -S sh -c 'exec "$0.venv/bin/python3" "$0" "$@"'
''''nix build --impure --expr 'with (import (builtins.getFlake "nixpkgs") {}); pkgs.python3.withPackages (p: with p; [feedparser])' -o sourcename.py.venv
exit
'''
import feedparser
```
Running `bash sourcename.py` from the data directory will ignore the shebang, run the `nix build` command to set up the venv, and exit.
Running `./sourcename.py` will interpret the shebang and use `sourcename.py.venv/bin/python3` as the interpreter.
You may have to adjust the `nix build` command to fit your environment.