Back to All Posts

A Simple Script to Initialize nix-Based Projects

These days any development I do for myself will use nix to, at the very least, define the development environment, if not the entire build process. I also always pin the `nixpkgs` version on a per-projecct basis, so that when I come back to the project 2 years later I don't have to deal with compatibility issues which have been sprung up in the meantime.

Pinning nixpkgs involves going to the nixpkgs GitHub page, finding the latest tagged release (or choosing tip of master, if I'm feeling frisky), finding the _commit_ of that tag, and then writing up a `default.nix` file which pulls this commit down and imports it. Usually at that point I remember that `builtins.fetchGit` isn't good for this, because it will do a full clone and nixpkgs is massive, so I have to go back and switch it to `builtins.fetchURL`. But then `builtins.fetchURL` needs a sha256, so it takes an extra step to grab that.

In short, it's a chore.

I could use nix flakes, which would simplify this process a bit, but then I'd have to remember the format of nix flakes' magical URL which lets you skip all this. And the structure of a nix flake in general. I don't really like nix flakes for other reasons, so rather than learning these things I've developed a simple bash script to solve the problem.

init-project: A simple script for initializing a project with a `default.nix`

At present the script does only a couple things:

- Grab the latest tagged release of nixpkgs

- Determine its commit

- Fetch the archive version of that commit, in order to determine its sha256

- Put together a basic `default.nix` file based on all of the above

- Initialize the directory as a git repository, and commit `default.nix`

Besides `nix` the only dependency is `git`. The generated `default.nix` includes a basic `mkShell` which can be used via `nix-shell -A shell`.

Future additions could be to include a LICENSE file, though I don't have a favorite which I could default to, and perhaps to create a `Makefile` which can serve as an entrypoint for starting a dev shell, running build commands, etc...

-----

Published 2024-06-08


This site is a mirror of my gemini capsule. The equivalent gemini page can be found here, and you can learn more about gemini at my 🚀 What is Gemini? page.


Hi! My name is Brian and I'm available for short-term remote contract work. Whether you need a consultant to guide you through a difficult problem or another pair of hands to smash out some code, I can do it all. Learn more about me and my skillset by browsing around this site, head over to my resume to find my actual work history, and shoot me an email when you're ready to get in touch.