Nix-init - Generate Nix packages from URLs with hash prefetching, dependency inference, license detection, and more

Features

  • Hash prefetching powered by nurl with support for cargoHash and vendorHash
  • Dependency inference for Rust, Go, and Python projects
  • Interactive prompts with fuzzy tab completions
  • License detection
  • Supported builders
    • stdenv.mkDerivation
    • buildRustPackage
    • buildPythonApplication and buildPythonPackage
    • buildGoModule
  • Supported fetchers
    • fetchCrate
    • fetchFromGitHub
    • fetchFromGitLab
    • fetchFromGitea
    • fetchPypi
    • All other fetchers supported by nurl are also supported, you just have to specify the tags manually

Usage

Usage: nix-init [OPTIONS] [OUTPUT]

Arguments:
  [OUTPUT]  The path or directory to output the generated file to

Options:
  -u, --url <URL>          Specify the URL
  -n, --nixpkgs <NIXPKGS>  Path to nixpkgs (in nix)
  -c, --config <CONFIG>    Specify the config file
  -h, --help               Print help
  -V, --version            Print version
38 Likes

And I thought nurl was cool enough…

4 Likes

Any plans for a flakes version?

2 Likes

nix-init generates callPackage friendly files which can be used in flakes. I don’t plan to support anything specific to flakes since the flake format doesn’t correspond to one single package, which nix-init tries to generate. Also note that currently nix-init only supports non-local projects (src = fetchFromGitHub ... instead of src = ./.), so it might be more helpful fore NUR-like multi-package flakes.

If you are looking for something to reduce the boilerplate of creating a new flake, check out nix flake init or nix-template. If you are looking for a common UI to build something using flakes, check out dream2nix which also has a simple template that can be used with nix flake init.

6 Likes

these are life saviors, thanks!

i’m of course going try it out myself, but for the curious: for the similar derivation/package declaration functionality, how does it compares to nix-template? for my quick view, yours seem to have greater automation with dependency inference.

2 Likes

I only learned about nix-template recently so correct me if I’m wrong.

In terms of package declaration functionally, it looks like nix-template’s features are more dependent on the selected template and fetcher, I didn’t get any python dependencies when I chose to use black from github instead of pypi. nix-init also has more features in this aspect, e.g. detecting the language/build system so you don’t have to pick the template yourself, dependency inference for rust dependencies, automatically filling in cargoHash and vendorHash, changelogs, and mixing buildPythonApplication or mkDerivation with cargoSetupHook and cargoDeps.

Something nix-template does that nix-init doesn’t is nixpkgs integration - it automatically creates directories in nixpkgs based on the template and tells you how to add the package to top level. This is something I don’t plan to implement for nix-init unless nixpkgs change its directory structure (i.e. RFC140). nix-template also supports creating (plain text) templates without a project, which can be helpful to reduce some boilerplate.

Something fundamentally different is that nix-init is interactive and guesses stuff like the template you want to use and has fzf-like tab completion (with rustyline and skim), and nix-template has support for non-package templates like NixOS modules and flakes.

Some smaller differences are that nix-template has a Qt template which nix-init doesn’t have, and nix-init has a wider support of fetchers

3 Likes

Damn this is slick! We need this kind of thing built into Nix (or advertised via Nix, wrapping a flake, like nix bundle). (Guix has something like this in guix import, as you may know.)

This is awesome! I can’t wait to try it.

2 Likes

Glad you liked it! I doubt this will make its way into nix, but it would be nice to have a more scriptable (instead of plain text) nix flake init that can work like guix import.

2 Likes

Nice!

Suggestion: add a section how to install it.

3 Likes

thanks for the suggestion, added an installation section: GitHub - nix-community/nix-init: Generate Nix packages from URLs with hash prefetching, dependency inference, license detection, and more [maintainer=@figsoda]

2 Likes

This is like ChatGPT for nixpkgs.

7 Likes

nix-init v0.1.1 has been released with the following changes:

Fixes

  • Fix compatibility with nixpkgs 22.11 (#15)
  • GitHub: sort tags by chronological order (#18)
  • Python: fix dependency parsing (#22)

Features

  • --nixpkgs to override nixpkgs (#14)
  • Handle deprecated spdx license identifiers
  • Python: detect build dependencies in build-system.requires (#23)
2 Likes

nix-init v0.2.0 has been released with a newly published crate and a new demo GIF

Features

  • Rust: support importCargoLock
  • Rust: improve dependency inference, it now sets environment variables and is feature-aware
  • Prompt for output path when it is unspecified
  • Go: infer ldflags from GoReleaser configuration
  • Python: recognize maturinBuildHook
  • Improve builder completions and validator
  • Improve description normalization
  • Improve version inference

Changes

  • Python: update buildPythonPackage’s style to be more conventional
  • Disallow empty urls
  • Normalize pname

Fixes

  • PyPI: support optional dependencies (#34)
  • PyPI: support zip sdists (#33)
  • PyPI: don’t strip digits from package names (#35)
  • PyPI: handle normalized sdist file names (#32)
  • PyPI: accept packages without licenses (#32)
  • PyPI: filter out non-sdist versions
  • Go: detect empty vendor from go.sum instead of FOD hash
8 Likes

nix-init v0.2.1 has been released in preparation for upcoming changes to Rust packaging in nixpkgs

Diff: Comparing v0.2.0...v0.2.1 · nix-community/nix-init · GitHub

Features

  • Rust: default to importCargoLock if git sources were found
  • Rust: deduplicate outputHashes based on git revisions
5 Likes

nix-init v0.2.2 released with a new Matrix chat and logo

Features

  • Logo and Matrix chat
  • Rust: use the first git dependency when multiple have the same revisions
  • Ask to fetch submodules
  • Improve error messages
4 Likes

nix-init v0.2.3 released with the following changes:

Features

  • Python: improve name normalization

Fixes

  • Python: fix parsing requirements.txt (#111)
  • Python: acknowledge requirements.txt when pyproject.toml is absent
8 Likes

nix-init v0.2.4 is released with dependency inference support for Go!

Features

  • Go: support dependency inference
  • Rust: improve dependency inference for the following crates: alsa-sys, curl-sys, gtk-sys, gtk4-sys, librocksdb-sys, llvm-sys
  • Go: improve ldflags formatting
  • Add nix and nurl to runtime with environment variables instead of relying on a wrapper

Changes

  • Drop support for nixpkgs 22.11

Fixes

  • Rust: use cargo and rustc instead of rustPlatform.rust.cargo and rustPlatform.rust.rustc
  • Python: fix pythonImportsCheck
  • Python(pyproject): default build-system.requires to setuptools
  • Fix interaction with showAliases = false on nixpkgs 23.11 (#153)
10 Likes

v0.3.0 has been released!

Highlights

  • support for RFC 140 pkgs/by-name (RFC, implementation). Thanks @infinisil and the Nixpkgs Architecture Team for working on this amazing feature in nixpkgs! A brand new commit option is added for this to commit the changes if it looks like it is by-name.
  • changes to python packaging (format → pyproject)
  • set mainProgram by default (motivation)

Full Changelog

Features

  • pkgs/by-name support: sensible defaults for the output path, and a commit option to automatically commit the changes
  • set meta.mainProgram by default
  • Zig: support zig.hook
  • Python: also add wheel when using setuptools as the build backend
  • add a meaningful comment when no licenses were found
  • Rust: improve dependency inference for the following crates: clipboard_macos, gspell-sys, libhandy-sys, libpanel-sys, libseat-sys, locate-dwarf, poppler-sys-rs, readkey, readmosue, soup-sys, soup2-sys, soup3-sys, sourceview4-sys, tracker-sys, trash, vte4-sys, webkit6-sys, wholesym, wireplumber, x11, xcb
  • Go: support the following fields in GoReleaser templates: .IsGitDirty, .PrefixedSummary, .Summary
  • Go: improve dependency inference for gotk4
  • mkDerivation: set meta.platforms by default
  • improve documentation for the nixpkgs option

Changes

  • Python: use pyproject = true instead of format = "..." (no longer suggests format = "setuptools")
  • prompt for the output path last
  • Go: remove dependency inference for glfw due to false positives

Fixes

  • Go: fix parsing of GoReleaser configuration files when ldflags is a string instead of a list
22 Likes

This is one of my favorite Nix tools, great work

4 Likes

It would be good to generate the file but from a compressed file found in the version releases, so that it is not necessary to compile the whole binary

1 Like