A simple helper that makes it easy to package Nu scripts with Nix, including inline dependency declarations for external commands and other packaged scripts.
flake nu nushell nix
1

Configure Feed

Select the types of activity you want to include in your feed.

Nix 91.7%
Nushell 8.3%
31 1 0

Clone this repository

https://tangled.org/csw.im/nu2nix https://tangled.org/did:plc:vmtvzn6xb55pvf3wkb7u37ks
git@knot.csw.im:csw.im/nu2nix git@knot.csw.im:did:plc:vmtvzn6xb55pvf3wkb7u37ks

For self-hosted knots, clone URLs may differ based on your setup.



README.md

nu2nix#

nu2nix is a simple helper that makes it easy to package Nu scripts with Nix, including inline dependency declarations for external commands and other packaged scripts.
Inspired by Python's Inline Script Metadata (PEP 723).

Why nu2nix?#

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 makes it harder to package collections of scripts programmatically, such as using builtins.readDir and mapping over the result. That was the original use case that motivated nu2nix.

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 scope. 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.

Try it Out#

The example directory contains an example of how to use nu2nix. You can also run this example directly, like so:

nix run git+https://tangled.org/csw.im/nu2nix?dir=example#example

Usage#

First, add nu2nix to your flake's inputs:

{
  inputs = {
    # ...
    nu2nix.url = "git+https://tangled.org/csw.im/nu2nix";
  };
}

Then, you can use inputs.nu2nix.lib.mkNuScript to package your scripts, like so:

{
  outputs = { self, nixpkgs, ... }@inputs: {
    # assuming eachSystem is defined elsewhere and a Nu script is located at `./scripts/example.nu` relative to your flake
    packages = eachSystem (system: {
      nu-example-script = inputs.nu2nix.lib.mkNuScript {
        pkgs = import nixpkgs { inherit system; };
        inherit self inputs;
        script = ./scripts/example.nu;
      };
    })
  };
}

External Dependencies#

nu2nix supports a custom format for declaring external dependencies inline in your script files, using an External Dependencies block comment.

The External Dependencies block comment must:

  • start with START: External Dependencies.
  • end with END: External Dependencies.
  • contain no more than one (1) dependency per line.

All declared dependencies are automatically added to the script's $PATH when executed, using a wrapper script.

#!/usr/bin/env nu

# START: External Dependencies
# git
# nix
# inputs.niks3.niks3
# self.some-helper
# END: External Dependencies

^git --version
^nix --version
WARNING

Only dependencies declared in the External Dependencies block comment or passed through mkNuScript's extraDependencies argument are available at runtime.
nu2nix replaces $PATH entirely instead of extending it. This makes scripts reproducible; if a command is not explicitly declared, it will not be available.
Nushell itself is the exception, as it's automatically added by mkNuScript.

Dependency Formats#

Scope Usage Example Resolves to mkNuScript Argument
Default (pkgs) git pkgs.git pkgs (raises error when missing)
inputs inputs.vicinae.default inputs.vicinae.packages.${system}.default inputs (raises error when missing & referenced)
self self.helper-script self.packages.${system}.helper-script self (raises error when missing & referenced)