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.

Where Next?

Popular General Dev topics Top

First poster: dwaynebradley
Maybe it’s just my experience, but Object-Oriented Programming seems like a default, most common paradigm of software engineering. The on...
New
First poster: AstonJ
We engineered a wearable microphone jammer that is capable of disabling microphones in its user’s surroundings, including hidden micropho...
New
First poster: bot
Kinesis Advantage360 Ergonomic Keyboard. Split-adjustable, contoured design that maximizes comfort and boosts productivity. Mechanical s...
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
CommunityNews
…or, “why make programming even harder?” Learning functional programming is an opportunity to discover a new way to represent programs, t...
New
First poster: bot
GitHub - lucidrains/PaLM-rlhf-pytorch: Implementation of RLHF (Reinforcement Learning with Human Feedback) on top of the PaLM architectur...
New
CommunityNews
The First Social-Media Babies Are Growing Up—And They’re Horrified. How would you feel if millions of people watched your childhood tant...
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: joni
My experience trying to write original, full-length human-sounding articles using Claude AI. You can use AI tools like Claude to help yo...
New
CommunityNews
A Brief Review of the Minisforum V3 AMD Tablet. Update: I have created an awesome-minisforum-v3 GitHub repository to list information fo...
New

Other popular topics Top

AstonJ
I’ve been hearing quite a lot of comments relating to the sound of a keyboard, with one of the most desirable of these called ‘thock’, he...
New
AstonJ
Inspired by this post from @Carter, which languages, frameworks or other tech or tools do you think is killing it right now? :upside_down...
New
Exadra37
I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New
New
PragmaticBookshelf
Author Spotlight Jamis Buck @jamis This month, we have the pleasure of spotlighting author Jamis Buck, who has written Mazes for Prog...
New
PragmaticBookshelf
Author Spotlight Mike Riley @mriley This month, we turn the spotlight on Mike Riley, author of Portable Python Projects. Mike’s book ...
New
PragmaticBookshelf
Author Spotlight: VM Brasseur @vmbrasseur We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New
PragmaticBookshelf
Author Spotlight: Peter Ullrich @PJUllrich Data is at the core of every business, but it is useless if nobody can access and analyze ...
New
PragmaticBookshelf
Author Spotlight: Sophie DeBenedetto @SophieDeBenedetto The days of the traditional request-response web application are long gone, b...
New
Fl4m3Ph03n1x
Background Lately I am in a quest to find a good quality TTS ai generation tool to run locally in order to create audio for some videos I...
New