From e242095aba69a9ed8c0fbe65030845d5078dde08 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Mon, 24 Feb 2025 13:04:10 -0800 Subject: [PATCH] Add shebang trick to documentation --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index cf339b8..963bfb9 100644 --- a/README.md +++ b/README.md @@ -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.