Select the types of activity you want to include in your feed.
A simple helper that makes it easy to package Nu scripts with Nix, including inline dependency declarations for external commands and other packaged scripts.
···33nu2nix is a simple helper that makes it easy to package [Nu scripts](https://www.nushell.sh/book/scripts.html) with Nix, including inline dependency declarations for external commands and other packaged scripts.
44Inspired by Python's [Inline Script Metadata](https://packaging.python.org/en/latest/specifications/inline-script-metadata/) ([PEP 723](https://peps.python.org/pep-0723/)).
5566+## Why uv2nix?
77+88+Nu scripts are great for small utilities and automation tasks, but packaging them with Nix means maintaining a separate Nix expression that actually builds your script into a runnable Nix package. Your dependency information ends up living in that Nix expression instead of with your script. This also ends up making it more difficult to, say, package a directory of scripts using `builtins.readDir` and some mapping magic, which is the original use case I created nu2nix for.
99+1010+nu2nix fixes these problems by keeping your dependency information stored inside your Nu scripts themselves. Just point nu2nix at a script file and give it a package set. It'll scan your script for an `External Dependencies` block comment and parse it, then the resulting package will have all of the dependencies you declared in your block comment in `$PATH`. nixpkgs (`pkgs`), flake inputs (`inputs`), and a flake's `self` argument are all supported as dependency sources.
1111+612## Try it Out
713814The [`example` directory](./example) contains an example of how to use nu2nix. You can also run this example directly, like so:
···43494450## External Dependencies
45514646-nu2nix supports a custom format for declaring external dependencies inline in your script files.
5252+nu2nix supports a custom format for declaring external dependencies inline in your script files.
5353+All declared dependencies are automatically added to the script's `$PATH` when executed, using a wrapper script.
5454+5555+> ![NOTE]
5656+> Only dependencies you declare in the `External Dependencies` comment or the `extraDependencies` argument to `mkNuScript` will be included in the script's `$PATH` wrapper. `$PATH` is completely overridden, not appended to. This helps with reproducibility; if an external command isn't explicitly included in the wrapper, Nushell won't be able to find it at runtime. The only exception to this is Nushell itself, as Nushell is implicitly added to the script's `$PATH` wrapper by `mkNuScript`.
47574858```nu
4959#!/usr/bin/env nu
···5464# inputs.niks3.niks3
5565# self.some-helper
5666# END: External Dependencies
6767+6868+^git --version
6969+^nix --version
5770```
58715959-Dependencies beginning with `inputs` are resolved from the `inputs` argument passed to `mkNuScript`. For example, `inputs.niks3.niks3` is equivalent to`inputs.niks3.packages.${pkgs.stdenv.hostPlatform.system}.niks3`. The `system` and `packages` attribute are automatically inserted.
6060-`self` works the same as `inputs`, but refers to the consuming flake. So, if you have a package you want to reuse in your Nu scripts that you declare in your flake, you can use this to refer to it.
6161-Do note that using `inputs`/`self` in a Nu script's external dependencies block will result in an error if you don't pass `inputs`/`self` to `mkNuScript`.
6262-Unqualified entries, such as `git` and `nix` in the example above, are pulled from the package set passed to `mkNuScript`. This will usually be nixpkgs.
7272+### Dependency Formats
63736464-All declared dependencies are automatically added to the script's `$PATH` when executed, using a wrapper script.
7474+| Scope | Usage Example | Resolves to | `mkNuScript` Argument |
7575+| :---------------: | :----------------------: | :-----------------------------------------: | :------------------------------------------------: |
7676+| _`<unqualified>`_ | `git` | `pkgs.git` | `pkgs` (raises error when missing) |
7777+| `inputs` | `inputs.vicinae.default` | `inputs.vicinae.packages.${system}.default` | `inputs` (raises error when missing & referenced ) |
7878+| `self` | `self.helper-script` | `self.packages.${system}.helper-script` | `self` (raises error when missing & referenced ) |