Make creating nix expressions easy. Provide a nice way to create largely boilerplate nix-expressions.
$ nix-template template rust --from-url github.com/jonringer/nix-template ./package.nix
Determining latest release for nix-template
Determining sha256 for nix-template
Prefetching cargoHash for nix-template (this may take a while)...
Determined cargoHash = sha256-cLSGWOyBQLv235TeYqSVg/f0Zmcnpj+RshINN69JYEU=
Materialising source to inspect Cargo.toml/Cargo.lock...
Inferred 1 buildInputs (["openssl"]) and 1 nativeBuildInputs (["pkg-config"])
Generated a rust nix expression at ./package.nixYou can also pass a URL directly as the first argument for auto-detection:
$ nix-template template https://github.com/jonringer/nix-template ./package.nixThe resulting file:
{ lib
, rustPlatform
, fetchFromGitHub
, pkg-config
, openssl
}:
rustPlatform.buildRustPackage (finalAttrs: {
pname = "nix-template";
version = "0.4.1";
src = fetchFromGitHub {
owner = "jonringer";
repo = finalAttrs.pname;
rev = "v${finalAttrs.version}";
sha256 = "sha256-42u5FmTIKHpfQ2zZQXIrFkAN2/XvU0wWnCRrQkQzcNI=";
};
cargoHash = "sha256-cLSGWOyBQLv235TeYqSVg/f0Zmcnpj+RshINN69JYEU=";
nativeBuildInputs = [
pkg-config
];
buildInputs = [
openssl
];
meta = with lib; {
description = "Make creating nix expressions easy";
homepage = "https://github.com/jonringer/nix-template";
license = licenses.cc0;
maintainers = with maintainers; [ jonringer ];
};
})# only need to config once per user
$ nix-template config name jonringer
$ nix-template config nixpkgs-root /home/jon/projects/nixpkgs
# add a package (using RFC 140 by-name structure), inferring template and dependencies
$ nix-template template auto --by-name --from-url github.com/jonringer/nix-template# Initialize as a flake project (auto-detects project type from local files)
$ nix-template project flake
# Initialize with npins dependency management
$ nix-template project npins
# Initialize with both flake and npins
$ nix-template project flake --with-npins
# Specify a template explicitly
$ nix-template project flake rustRunning nix-template with no arguments enters interactive mode, which guides you through template selection and configuration.
Use the auto template to automatically detect project type from source code:
$ nix-template template auto --from-url github.com/user/project
# Automatically detects if it's Rust, Go, Python, UV, npm, pnpm, PHP, .NET, or RubyStandard Templates:
stdenv/stdenvNoCC- Generic stdenv-based buildspython_package/python_application- Python packages (buildPythonPackage/buildPythonApplication)rust- Rust packages (rustPlatform.buildRustPackage)go- Go modules (buildGoModule)npm/pnpm- Node.js packagesphp- PHP packages with Composer (php.buildComposerProject2)- Uses generic
phpattribute (auto-tracks nixpkgs default version) - Automatically detects PHP extensions from
composer.json - Generates
php.buildEnvwrapper when extensions are required - Detects version requirements (e.g.,
"php": "^8.2") to use specific versions when needed
- Uses generic
dotnet- .NET packagesruby- Ruby gemsmkshell- Development shellsmodule- NixOS modulestest- NixOS integration tests
Modern Packaging Variants:
uv- Python projects using UV package manager (detected viauv.lock)- Note: UV projects work best with flake-based workflows. Initialize with:
nix flake init -t github:pyproject-nix/uv2nix#hello-world
- Note: UV projects work best with flake-based workflows. Initialize with:
rust_crane- Rust builds with incremental caching via crane- Better incremental builds and caching than buildRustPackage
- See: https://crane.dev/
go_gomod2nix- Go builds with better dependency sharing- Uses gomod2nix.toml for dependency management
- See: https://github.com/nix-community/gomod2nix
Automatically infers dependencies for supported languages:
- Rust: Scans
Cargo.tomlandCargo.lockfor native dependencies - Go: Detects CGO directives and maps to nixpkgs inputs
- PHP: Detects extensions (
ext-*) and native libraries fromcomposer.json - Ruby: Maps gems from
Gemfile.lockto nixpkgs dependencies - CMake/Meson: Parses build files for common dependencies
- Python: Fetches dependencies from PyPI metadata
Use --skip-infer-deps to disable this feature.
Automatically prefetches and calculates vendor hashes for:
- Rust (
Cargo.lock) - Go (
go.sum) - npm (
package-lock.json) - pnpm (
pnpm-lock.yaml)
Use --skip-vendor-hash to disable this feature.
Supports fetching from:
- GitHub
- GitLab
- Gitea
- PyPI
Use --by-name flag to generate packages using the modern pkgs/by-name directory structure.
Initialize new projects with flake or npins-based setups (will prompt you for additional information):
$ nix-template project flake
$ nix-template project npins
$ nix-template project flake --with-npinsfrom nixpkgs (unstable, not available in 20.03):
$ nixenv -iA nix-template
with nix-cli (from this repository):
$ nix-env -f default.nix -iA ""
with cargo
$ cargo install --path .
using flakes
$ nix run github:jonringer/nix-template
Installing depedencies on nixpkgs:
nix-shell
# or
nix develop
Other platforms, you'll need the following dependencies:
- cargo
- rustc
- rust-clippy