CommunityNews

CommunityNews

Throwaway development environments with Nix

Throwaway development environments with Nix.
I use Nix a lot.
Nix is a bunch of different things. It’s a programming language, designed for expressing a build pipeline. It’s a package manager. It (well, NixOS) is an operating system based on that package manager.
Today I’d like to talk about the package manager. Specifically, a lovely gateway into the rest of the ecosystem, nix-shell.
Some people will tell you that the point of Nix is to set up your software so it can be built with Nix, which allows you to tightly control all dependencies and emit something that is as close to reproducible as possible. I am all for this, but if we can tightly control the dependencies without actually building inside a Nix environment, we’ve still improved the reproducibility a lot, and it’s not that hard.
To follow along, you’ll need to first install Nix. It works on Linux and macOS; if you use Windows, I recommend checking out the Windows Subsystem for Linux.
Running an arbitrary program with nix-shell
nix-shell does two things. It will read an environment specification from a file named shell.nix and load up a bash shell (which you can override) with that environment present. Or it will do the same thing with a list of packages supplied on the command line.
For example, if I want to run the cowsay program, I don’t have to install it: I can load a shell with that program.
$ nix-shell -p cowsay
this path will be fetched (0.01 MiB download, 0.05 MiB unpacked):
/nix/store/x87xaaad225x5x9gv15mn01mf204kycv-cowsay-3.04
copying path ‘/nix/store/x87xaaad225x5x9gv15mn01mf204kycv-cowsay-3.04’ from ‘https://cache.nixos.org’…

[nix-shell:~]$ cowsay hello


< hello >

    \   ^__^
     \  (oo)\_______
        (__)\       )\/\
            ||----w |
            ||     ||

Running nix-shell -p cowsay will download cowsay from the nixpkgs package repository, and then launch a bash shell with that program available, by adding it to the PATH.
We can verify that:
[nix-shell:~]$ echo $PATH
…:/nix/store/x87xaaad225x5x9gv15mn01mf204kycv-cowsay-3.04/bin:…
There it is: cowsay, downloaded to the Nix store.
(You’ll see more than just that in the PATH, and you may see nix-shell download a lot of extra packages the first time you use it. Just sit tight.)
You may be wondering what that long string of letters and numbers is in the package directory name. It’s a hash of all the inputs, which means that if one of the dependencies change, the hash will change too. The source of the program is also considered an input, so if we upgrade cowsay to a new version (I find it awesome that there are so many versions of this program), the hash will also change.
This means that we can have various different versions all available in our local store at once, and they won’t collide with each other. Perhaps not so important for cowsay, but when we start dealing with programming languages, this gets interesting.

Read in full here:

This thread was posted by one of our members via one of our news source trackers.

Popular General Dev topics Top

AstonJ
Apple co-founder Steve Wozniak is suing YouTube for allegedly allowing scammers to use images and videos of him to defraud people. The s...
New
New
First poster: dpritchett
It’s not what programming languages do, it’s what they shepherd you to. How many of you have listened, read or taken part in a discussio...
New
First poster: mikhailgrib
Let’s build GPT: from scratch, in code, spelled out… We build a Generatively Pretrained Transformer (GPT), following the paper “Attentio...
New
First poster: dani
The pool of talented C++ developers is running dry. Highly sought after, rarely provided.
New
First poster: Korbin73
Whatever happened to Elm, anyway?. I see this question pop up quite frequently in lots of different arenas - folks are curious as to wha...
New
First poster: bot
When Zig is safer and faster than Rust. There are endless debates online about Rust vs. Zig, this post explores a side of the argument I...
New
First poster: bot
Declarative GNOME configuration with NixOS. I adore tinkering with my machine, trying new tools, extensions, themes, and ideas. When I w...
New
First poster: FatimaAdamu
Two US lawyers fined for submitting fake court citations from ChatGPT. Law firm also penalised after chatbot invented six legal cases th...
New
First poster: DevotionGeo
To avoid being replaced by LLMs, do what they can’t. What LLM’s can’t do yet
New

Other popular topics Top

Devtalk
Hello Devtalk World! Please let us know a little about who you are and where you’re from :nerd_face:
New
siddhant3030
I’m thinking of buying a monitor that I can rotate to use as a vertical monitor? Also, I want to know if someone is using it for program...
New
Rainer
My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
New
AstonJ
poll poll Be sure to check out @Dusty’s article posted here: An Introduction to Alternative Keyboard Layouts It’s one of the best write-...
New
AstonJ
I have seen the keycaps I want - they are due for a group-buy this week but won’t be delivered until October next year!!! :rofl: The Ser...
New
Maartz
Hi folks, I don’t know if I saw this here but, here’s a new programming language, called Roc Reminds me a bit of Elm and thus Haskell. ...
New
First poster: joeb
The File System Access API with Origin Private File System. WebKit supports new API that makes it possible for web apps to create, open,...
New
AstonJ
Was just curious to see if any were around, found this one: I got 51/100: Not sure if it was meant to buy I am sure at times the b...
New
AstonJ
If you want a quick and easy way to block any website on your Mac using Little Snitch simply… File &gt; New Rule: And select Deny, O...
New
First poster: bot
Large Language Models like ChatGPT say The Darnedest Things. The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New