Make creating nix expressions easy

Overview

Nix-template

NOTE: This is still WIP, but should be useful in most situations

Make creating nix expressions easy. Provide a nice way to create largely boilerplate nix-expressions.

Roadmap

  • Finalize cli semantics
  • Ease usage with nixpkgs repo
    • Write to correct location using path
      • Improve logic around directories vs files
      • Improve template-specific items
        • generate buildGoModule's depsSha256
        • generate buildRustPackages's cargoSha256
    • Print top-level addition statement
  • Support Language/frameworks/usage templates:
    • Stdenv
    • Python
    • mkShell
    • Qt
    • Go
    • Rust
    • Flakes
    • NixOS Module
    • NixOS Test
    • Haskell
    • and many more...
  • Add option (--comments?) to embed noob-friendly comments and explanations about common usage patterns
  • Allow contributor information to be set locally (similar to git settings)
    • Set maintainer name through $XDG_CONFIG_HOME
    • Set nixpkgs-root path through $XDG_CONFIG_HOME
  • Better integration with fetchers
    • Automatically determine version and sha256
      • Github (need a way to pass owner and repo)
      • Pypi (will need a way to pass pypi pname, as it may differ from installable path)
  • Implement shell completion (nix-template completions )

Current Usage (github and pypi only)

$ nix-template rust -n --from-url github.com/jonringer/nix-template
Creating directory: /home/jon/projects/nixpkgs/pkgs/applications/misc/nix-template
Generating python expression at /home/jon/projects/nixpkgs/pkgs/applications/misc/nix-template/default.nix
Please add the following line to the approriate file in top-level:

  nix-template = callPackage ../applications/misc/nix-template { };

The resulting file:

# $NIXPKGS_ROOT/pkgs/applications/misc/nix-template/default.nix
{ lib, rustPlatform, fetchFromGitHub }:

rustPlatform.buildRustPackage rec {
  pname = "nix-template";
  version = "0.1.0";

  src = fetchFromGitHub {
    owner = "jonringer";
    repo = pname;
    rev = "v${version}";
    sha256 = "1h6xdvhzg7nb0s82b3r5bsh8bfdb1l5sm7fa24lfwd396xp9yyig";
  };

  cargoSha256 = "0000000000000000000000000000000000000000000000000000";

  buildInputs = [ ];

  meta = with lib; {
    description = "Make creating nix expressions easy";
    homepage = "https://github.com/jonringer/nix-template/";
    license = licenses.cc0;
    maintainers = with maintainers; [ jonringer ];
  };
}

Current Usage (Generically)

# only need to config once per user
$ nix-template config name jonringer
$ nix-template config nixpkgs-root /home/jon/projects/nixpkgs

# add a package
$ nix-template python --pname requests -f pypi -l asl20
Creating directory: /home/jon/projects/nixpkgs/pkgs/development/python-modules/requests/
Generating python expression at /home/jon/projects/nixpkgs/pkgs/development/python-modules/requests/default.nix
Please add the following line to the approriate file in top-level:

  requests = callPackage ../development/python-modules/requests { };
# pkgs/development/python-modules/requests/default.nix
{ lib, buildPythonPackage, fetchPypi }:

buildPythonPackage rec {
  pname = "requests";
  version = "0.0.1";

  src = fetchPypi {
    inherit pname version;
    sha256 = "0000000000000000000000000000000000000000000000000000";
  };

  propagatedBuildInputs = [ ];

  pythonImportsCheck = [ "requests" ];

  meta = with lib; {
    description = "CHANGEME";
    homepage = "https://github.com/CHANGEME/requests/";
    license = licenses.asl20;
    maintainer = with maintainers; [ jonringer ];
  };
}

Installation

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

Development

Installing depedencies on nixpkgs:

nix-shell

Other platforms, you'll need the following dependencies:

  • cargo
  • rustc
  • rust-clippy

End Goal

# only need to config once per user
$ nix-template config name jonringer
$ nix-template config nixpkgs-root /home/jon/projects/nixpkgs

# add a package
$ nix-template python --pname requests -f pypi -l asl20
Found latest stable release to be 2.24.0 on pypi.com
Creating directory: /home/jon/projects/nixpkgs/pkgs/development/python-modules/requests/
Generating python expression at /home/jon/projects/nixpkgs/pkgs/development/python-modules/requests/default.nix
For an addition to nixpkgs as a python package, please add the following to pkgs/top-level/python-packages.nix:

  requests = callPackage ../development/python-modules/<PNAME> { };

For an addition to nixpkgs as a python application, please add the following to pkgs/top-level/all-packages.nix:

  requests = python3Packages.callPackage <PATH_FROM_CLI> { };
# pkgs/development/python-modules/requests/default.nix
{ lib, buildPythonPackage, fetchPypi }:

buildPythonPackage rec {
  pname = "requests";
  version = "2.24.0";

  src = fetchPypi {
    inherit pname version;
    sha256 = "b3559a131db72c33ee969480840fff4bb6dd111de7dd27c8ee1f820f4f00231b";
  };

  propagatedBuildInputs = [ ];

  pythonImportsCheck = [ "requests" ];

  meta = with lib; {
    description = "CHANGEME";
    homepage = "https://github.com/CHANGEME/requests/";
    license = licenses.asl20;
    maintainer = with maintainers; [ jonringer ];
  };
}
Comments
  • completions fail to load

    completions fail to load

    i put

    eval "$(nix-template completions zsh)"
    

    in my zshrc

    i get _arguments: command not found when i start a new shell

    happens without a config too

    env -i zsh -d -f -i
    eval "$(nix-template completions zsh)"
    
    opened by Artturin 5
  • Add noob-friendly `--documentation-links` option

    Add noob-friendly `--documentation-links` option

    Hi @jonringer!

    I got to see your chat with @zimbatm on Nix friday, where you mentioned working on this project. Loved the idea, and reading through the README.md, I got inspired by the --comments item you left on your Roadmap.

    Not sure you accept contributions to this project, but I tried out something and figured you might be interested: This PR adds a --documentation-links (or -d) option to add links to relevant sections of the Nixpkgs contributor guide.

    And whether you want it or not: Thanks for nix-template!

    opened by Pamplemousse 3
  • add https:// to homepage github link

    add https:// to homepage github link

    before

    nix-template --stdout stdenv --from-url=https://github.com/varlink/libvarlink
    Determining latest release for libvarlink
    Determining sha256 for libvarlink
    { lib, stdenv, fetchFromGitHub }:
    
    stdenv.mkDerivation rec {
      pname = "libvarlink";
      version = "22";
    
      src = fetchFromGitHub {
        owner = "varlink";
        repo = pname;
        rev = "version";
        sha256 = "1i15227vlc9k4276r833ndhxrcys9305pf6dga1j0alx2vj85yz2";
      };
    
      buildInputs = [ ];
    
      meta = with lib; {
        description = "C implementation of the Varlink protocol and command line tool";
        homepage = "github.com/varlink/libvarlink";
        license = licenses.CHANGE;
        maintainers = with maintainers; [ artturin ];
      };
    }
    

    after

    ./result/bin/nix-template --stdout stdenv --from-url=https://github.com/varlink/libvarlink
    Determining latest release for libvarlink
    Determining sha256 for libvarlink
    { lib, stdenv, fetchFromGitHub }:
    
    stdenv.mkDerivation rec {
      pname = "libvarlink";
      version = "22";
    
      src = fetchFromGitHub {
        owner = "varlink";
        repo = pname;
        rev = "version";
        sha256 = "1i15227vlc9k4276r833ndhxrcys9305pf6dga1j0alx2vj85yz2";
      };
    
      buildInputs = [ ];
    
      meta = with lib; {
        description = "C implementation of the Varlink protocol and command line tool";
        homepage = "https://github.com/varlink/libvarlink";
        license = licenses.CHANGE;
        maintainers = with maintainers; [ artturin ];
      };
    }
    
    opened by Artturin 2
  • homepage metadata is not optional for pypi packages

    homepage metadata is not optional for pypi packages

    ➜ nix-template python --nixpkgs --pname mitmproxy_wireguard --from-url https://pypi.org/project/mitmproxy_wireguard
    Determining latest release for mitmproxy_wireguard
    [2022-12-07T17:55:37Z ERROR nix_template::url] Unable to parse response from pypi.io to json: Error { path: Path { segments: [Map { key: "info" }, Map { key: "project_urls" }] }, original: Error("missing field `Homepage`", line: 1, column: 6718) }
    
    opened by SuperSandro2000 1
  • Add default format to pypi packages

    Add default format to pypi packages

    format should always be set for new packages even when it defaults to setuptools because it is planned to remove the implicit default. I would suggest to add format = "setuptools"; and leave people to change it to pyproject or some other when it doesn't work.

    opened by SuperSandro2000 1
  • python: `invalid type: map, expected a sequence`

    python: `invalid type: map, expected a sequence`

    nix-template 0.4.0

    sh -c "nix-template python --fetcher github --from-url https://github.com/victorfs/parsifal"
    Determining latest release for parsifal
    [2022-09-21T12:20:00Z ERROR nix_template::url] Unable to parse response from github to json: Error { path: Path { segments: [] }, original: Error("invalid type: map, expected a sequence", line: 1, column: 0) }
    

    seems mostly an issue with python template. rust projects just works.

    opened by yuuyins 1
  • Crash when project on pypi has no platform

    Crash when project on pypi has no platform

    $ nix-template python --nixpkgs --pname avocado-framework --from-url https://pypi.org/project/avocado-framework
    No [PATH] provided, defaulting to "pkgs/development/python-modules/"
    Determining latest release for avocado-framework
    [2022-04-27T11:18:19Z ERROR nix_template::url] Unable to parse response from pypi.io to json: Error { path: Path { segments: [Map { key: "info" }, Map { key: "platform" }] }, original: Error("invalid type: null, expected a string", line: 1, column: 12039) }
    
    opened by SuperSandro2000 1
  • `PATH` argument does not accept directories

    `PATH` argument does not accept directories

    Help text for the PATH argument is as follows:

    <PATH>        directory or file to be written. In the case of a directory, a default.nix will be created.
    

    However, I couldn't get this working with directories:

    $ mkdir test
    $ nix-template module -p test test/
    Cannot write to file 'test/', already exists
    
    opened by ozkutuk 1
  • Instructions for aarch64

    Instructions for aarch64

    Trying to add nix-template to my home-manager configuration I get:

    $HOST = aarch64-unknown-linux-gnu $TARGET = aarch64-unknown-linux-gnu openssl-sys = 0.9.65 It looks like you're compiling on Linux and also targeting Linux. Currently this requires the pkg-config utility to find OpenSSL but unfortunately pkg-config could not be found. If you have OpenSSL installed you can likely fix this by installing pkg-config.

    I use this expression:

    nix-template =
      self.ever-given.buildRustPackage {
        src =
          super.fetchFromGitHub {
            inherit (sources.nix-template) owner repo rev sha256;
           }; 
      };
    
    opened by 573 1
  • --from-url github.com/YoyPa/isw fails

    --from-url github.com/YoyPa/isw fails

    nix-template Python --from-url github.com/YoyPa/isw
    Determining latest release for isw
    Determining sha256 for isw
    [2021-07-12T04:24:27Z ERROR nix_template::url] Unable to parse response from github to json: Error("invalid type: null, expected a string", line: 1, column: 4000)
    
    opened by Artturin 1
  • Add flake template

    Add flake template

    Would be nice to have an opinionated flake layout template

    {
      description = "CHANGEME";
    
      inputs = {
        utils.url = "github:numtide/flake-utils";
      };
    
      outputs = { self, nixpkgs, utils }:
        let
          # put devShell and any other required packages into local overlay
          localOverlay = import ./nix/overlay.nix;
          
          pkgsForSystem = system: import nixpkgs {
            overlays = [
              localOverlay
            ];
            inherit system;
          };
        in utils.lib.eachSystem [ "x86_64-linux" ] (system: rec {
          legacyPackages = pkgsForSystem system;
          packages = utils.lib.flattenTree {
            inherit (pkgs) devShell myPkg;
          };
          defaultPackage = packages.myPkg;
          apps.<mypkg> = utils.lib.mkApp { drv = packages.myPkg; }
      });
    }
    
    opened by jonringer 1
  • Create

    Create "init" subcommand

    Might be nice to create a guided prompt workflow for generating a nix expression or project, similar to npm init

    • Do want to create a new flake project? [y/n]
      • If yes, ask for what language
        • then render #51 layout
      • If no, ask for what template they would like
        • Ask for pname, version, license, maintainer
    opened by jonringer 0
  • Create

    Create "--flake-project" option

    Would be nice to be able to extend a template to give an opinionated flake project setup for a template:

    flake.nix
    flake.lock
    nix/
      overlay.nix
      <pname>.nix
    
    opened by jonringer 1
  • Allow way to pass many inputs

    Allow way to pass many inputs

    Allow some way to pass that many dependencies will be needed:

    E.g. nix-template -p amqp-cpp -n -d zlib -d openssl

    or

    nix-template -p amqp-cpp -n -- zlib openssl

    opened by jonringer 0
  • Find appropriate location to place addition in top-level attr set

    Find appropriate location to place addition in top-level attr set

    Would be nice that instead of just printing the attr addition line, that nix-template will just add the attr name at an appropriate location within the file.

    The steps would roughly be:

    • remove all comments,
    • split file on paragraphs,
    • then find location
    • insert new package reference
    opened by jonringer 0
Releases(v0.4.1)
  • v0.4.1(Dec 17, 2022)

    v0.4.1

    • Additions:

      • Python template defaults to adding a 'format = "setuptools";' to align with nixpkgs preferences
    • Fixes:

      • Clearer error when repository doesn't exist
      • Minor pypi serialization fixes
    Source code(tar.gz)
    Source code(zip)
  • v0.4.0(Sep 10, 2022)

    What's Changed

    • Breaking Changes:

      • overlay for flake template has been moved to overlays.default to align with upstream changes
      • -u will now use an sri hash, to align with nix 2.4+ behavior
    • Additions:

      • -u when fetching from pypi will now automatically add dependencies
    • Fixes:

      • Fix failure with pypi responses not containing a platform
      • -u with pypi will now filter out pre-releases when determining latest release
      • Default to repo name when using -u
    • Platform support:

        • Add support for aarch64-darwin by @veehaitch in https://github.com/jonringer/nix-template/pull/43

    New Contributors

    • @veehaitch made their first contribution in https://github.com/jonringer/nix-template/pull/43

    Full Changelog: https://github.com/jonringer/nix-template/compare/v0.3.0...v0.4.0

    Source code(tar.gz)
    Source code(zip)
  • v0.3.0(Apr 19, 2022)

    • Breaking Changes:

      • overlays exposed in flake are now an attr set, to better align with more recent nix versions
    • Additions:

      • aarch64-darwin added to flake system defaults
    • Improvements:

      • Serialization errors will now mention which assumption caused the failure @blaggacao
      • Updated github.com auto-detected licenses to include recently added Apache License 2.0
      • Fixed usage of mkApp inside flake template
    • Fixes:

      • Fix unprefixed versions being generated as version = "version"; @blaggacao
      • Fixed directories being passed as [PATH] not becoming dir/default.nix
    Source code(tar.gz)
    Source code(zip)
  • v0.2.0(Dec 28, 2021)

    • Breaking Changes / Behaviors:

      • Flake template now requires -p,--pname
      • Nix expresions now have input attrs in comma-leading style (one input per line)
    • Fixes:

      • --from-url no longer errors with --nixpkgs when a pname is not supplied
      • Fix differences of writing to stdout vs file
      • Update flake template (overlay usage)
      • Failures from already existing file locations occur sooner
        • Particularly irritating with --from-url, which would compute release and sha256 info
    Source code(tar.gz)
    Source code(zip)
  • v0.1.4(Jul 18, 2021)

  • v0.1.3(Jul 18, 2021)

  • v0.1.2(Jul 18, 2021)

    • Add -u, --from-url support to pypi.org
    • Fix crash when github's hompage url is null when used with -u
    • Add mention of GITHUB_TOKEN to usage
    Source code(tar.gz)
    Source code(zip)
  • v0.1.1(Jul 11, 2021)

  • v0.1.0(Aug 31, 2020)

Owner
Jonathan Ringer
A release manager for NixOS 20.09, 21.05, and 21.11. Functional Programming fan including Haskell and NixOS
Jonathan Ringer
A template for creating services in Rust using Axum and Prisma.

A template for creating services in Rust using Axum and Prisma. This uses the super cool Prisma Rust Client.

Aaron Leopold 6 Oct 19, 2022
A `nix` and `nix-shell` wrapper for shells other than `bash`

nix-your-shell A nix and nix-shell wrapper for shells other than bash. nix develop and nix-shell use bash as the default shell, so nix-your-shell prin

Mercury 15 Apr 10, 2023
Progress In Nix - Pacman inspired frontend for Nix

Progress In Nix Pinix is a Pacman inspired frontend for Nix. It wraps a regular Nix command and replaces the output with a more modern and informative

Rémi Dupré 23 Mar 9, 2024
Text Expression Runner – Readable and easy to use text expressions

ter - Text Expression Runner ter is a cli to run text expressions and perform basic text operations such as filtering, ignoring and replacing on the c

Maximilian Schulke 72 Jul 31, 2022
A command-line tool and library for generating regular expressions from user-provided test cases

Table of Contents What does this tool do? Do I still need to learn to write regexes then? Current features How to install? 4.1 The command-line tool 4

Peter M. Stahl 5.8k Dec 30, 2022
hado-rshado — A little macro for writing haskell-like do expressions without too much ceremony

hado Monadic haskell-like expressions brought to rust via the hado! macro What? A little macro for writing haskell-like do expressions without too muc

Lucas David Traverso 44 Jul 31, 2022
An implementation of regular expressions for Rust. This implementation uses finite automata and guarantees linear time matching on all inputs.

regex A Rust library for parsing, compiling, and executing regular expressions. Its syntax is similar to Perl-style regular expressions, but lacks a f

The Rust Programming Language 2.6k Jan 8, 2023
A Rust macro for writing nested loop expressions

loop_chain A Rust macro for writing nested loop expressions Usage | Examples | Docs Dependencies [dependencies] loop_chain = "0.1.1" Usage For express

Takayuki Maeda 5 Jul 30, 2021
Representing Wolfram Language expressions in Rust.

wolfram-expr Representation of Wolfram Language expressions. Examples Construct the expression {1, 2, 3}: use wolfram_expr::{Expr, Symbol}; let expr

Wolfram Research, Inc. 7 Aug 18, 2022
Optimize floating-point expressions for accuracy

Herbie automatically improves the error of floating point expressions. Visit our website for tutorials, documentation, and an online demo. Herbie has

Herbie Project 611 Dec 19, 2022
RnR is a command-line tool to securely rename multiple files and directories that supports regular expressions

RnR is a command-line tool to securely rename multiple files and directories that supports regular expressions. Features Batch rename files and direct

Ismael González Valverde 219 Dec 31, 2022
Rust library for regular expressions using "fancy" features like look-around and backreferences

fancy-regex A Rust library for compiling and matching regular expressions. It uses a hybrid regex implementation designed to support a relatively rich

fancy-regex 302 Jan 3, 2023
Generate progress bars from cron expressions.

jalm Generate Progress Bars from Cron Expressions Installation and Usage Grab the latest binary from the Github Actions tab. Alternatively, to build f

iamkneel 22 Oct 30, 2022
😎 Pretty way of writing regular expressions in Rust

?? Write readable regular expressions The crate provides a clean and readable way of writing your regex in the Rust programming language: Without pret

Adi Salimgereyev 7 Aug 12, 2023
Fast array expressions on the stack, no-std compatible

Strobe Fast, low-memory, elementwise array expressions on the stack. Compatible with no-std (and no-alloc) environments. This crate provides array exp

James Logan 3 Sep 23, 2023
Pet project to get acquainted with Rust, and mess around with symbolic expressions.

Symba Pet project to get acquainted with Rust, and to mess around with symbolic expressions, hence the name 'Symba'. Example: use asg::{deftree, r

Ranjeeth Mahankali 3 Nov 23, 2023
Asserts const generic expressions at build-time.

build_assert build_assert allows you to make assertions at build-time. Unlike assert and some implementations of compile-time assertions, such as stat

MaxXing 4 Nov 23, 2023
Victorem - easy UDP game server and client framework for creating simple 2D and 3D online game prototype in Rust.

Victorem Easy UDP game server and client framework for creating simple 2D and 3D online game prototype in Rust. Example Cargo.toml [dependencies] vict

Victor Winbringer 27 Jan 7, 2023
A Modern Real-Time Data Processing & Analytics DBMS with Cloud-Native Architecture, built to make the Data Cloud easy

A Modern Real-Time Data Processing & Analytics DBMS with Cloud-Native Architecture, built to make the Data Cloud easy

Datafuse Labs 5k Jan 9, 2023
A Modern Real-Time Data Processing & Analytics DBMS with Cloud-Native Architecture, built to make the Data Cloud easy

A Modern Real-Time Data Processing & Analytics DBMS with Cloud-Native Architecture, built to make the Data Cloud easy

Datafuse Labs 5k Jan 9, 2023