<img src="assets/logo.svg" align="right" height="280">

# nix-init

[![matrix](https://img.shields.io/matrix/nix-init:nixos.org?server_fqdn=matrix.org&style=flat-square)](https://matrix.to/#/#nix-init:nixos.org)
[![release](https://img.shields.io/github/v/release/nix-community/nix-init?logo=github&style=flat-square)](https://github.com/nix-community/nix-init/releases)
[![version](https://img.shields.io/crates/v/nix-init?logo=rust&style=flat-square)](https://crates.io/crates/nix-init)
[![license](https://img.shields.io/badge/license-MPL--2.0-blue?style=flat-square)](https://www.mozilla.org/en-US/MPL/2.0)
[![ci](https://img.shields.io/github/checks-status/nix-community/nix-init/main?style=flat-square)](https://buildbot.nix-community.org/#/builders/2728)

Generate Nix packages from URLs

> Note: It is likely that the generated package will not work without some
> tweaks, also remember to double check the license and description even if it
> does work

- 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

![](https://user-images.githubusercontent.com/40620903/226211877-2d583d09-4fbc-4869-8248-6166edde21cc.gif)

## Installation

The latest release of nix-init is packaged in nixpkgs and kept up to date on the
unstable branches

![](https://repology.org/badge/vertical-allrepos/nix-init.svg)

If you want to use a more recent snapshot of nix-init, it is also available as a
flake. The following command is equivalent to running `nix-init --help`:

```bash
nix run github:nix-community/nix-init -- --help
```

or if you don't have flakes enabled:

```bash
nix run --extra-experimental-features "flakes nix-command" github:nix-community/nix-init -- --help
```

## 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
      --rev <REV>                    Specify the tag or revision
  -S, --submodules[=<SUBMODULES>]    Always fetch submodules when possible [possible values: true, false]
  -V, --version [<VERSION>]          Specify the version of the package, or print nix-init version if no arguments are present
      --pname <PNAME>                Specify the pname
      --builder <BUILDER>            Specify the builder
      --cargo-vendor <CARGO_VENDOR>  Specify how the cargo dependencies are vendored [possible values: fetchCargoVendor, importCargoLock]
  -y, --overwrite[=<OVERWRITE>]      Always overwrite files [possible values: true, false]
      --headless                     Don't prompt for anything (requires --url)
  -n, --nixpkgs <NIXPKGS>            Path to nixpkgs (in nix)
  -C, --commit[=<COMMIT>]            Commit the changes if the output path is name-based (RFC 140) [possible values: true, false]
  -c, --config <CONFIG>              Specify the config file
  -h, --help                         Print help (see more with '--help')
```

### Supported builders

- `stdenv.mkDerivation` and `stdenvNoCC.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
  manually input the tag/revision of the package

## Configuration

nix-init will try to find `nix-init/config.toml` under XDG configuration
directories

```toml
# ~/.config/nix-init/config.toml

# maintainers that will get added to the package meta
maintainers = ["figsoda"]

# path to nixpkgs (in nix), equivalent to `--nixpkgs`
nixpkgs = "<nixpkgs>" # use the nixpkgs from channels (default)
# nixpkgs = 'builtins.getFlake "nixpkgs"' # use the nixpkgs from the flake registry

# commit the changes if the output path is name-based (RFC 140)
# see https://github.com/NixOS/nixpkgs/tree/master/pkgs/by-name for more information
commit = true

# access tokens to access private repositories and avoid rate limits
[access-tokens]
"github.com" = "ghp_blahblahblah..."
"gitlab.com".command = ["secret-tool", "or", "whatever", "you", "use"]
"gitlab.gnome.org".file = "/path/to/api/token"

# if unspecified, nix-init tries to format the output file with nixfmt if it's found in PATH
# the formatter should take a Nix file from stdin and output the formatted file to stdout
[format]
command = ["alejandra"]
```

## Changelog

See [CHANGELOG.md](CHANGELOG.md)

[nurl]: https://github.com/nix-community/nurl
