Nix is a powerful package manager for Linux and other Unix systems that makes package management reliable and reproducible. Share your development and build environments across different machines.
NixOS is a Linux distribution with a unique approach to package and configuration management. Built on top of the Nix package manager, it is completely declarative, makes upgrading systems reliable, and has many other advantages.
Download Get startedNix builds packages in isolation from each other. This ensures that they are reproducible and don't have undeclared dependencies, so if a package works on one machine, it will also work on another.
Nix makes it trivial to share development and build environments for your projects, regardless of what programming languages and tools you’re using.
Nix’s ensures that installing or upgrading one package cannot break other packages. It allows you to roll back to previous versions, and ensures that no package is in an inconsistent state during an upgrade.
The Nix Packages collection (nixpkgs) is a set of over 60 000 packages for the Nix package manager, released under a permissive MIT/X11 license.
Don't clutter your system with tools that you use only now and then.
python --version python: command not found nix-shell -p python3 (nix-shell)python --version Python 3.7.7
nix-shell -p python3 nodejs go rustc (nix-shell)node --version v10.20.1 (nix-shell)go version go version go1.14.1 linux/amd64 (nix-shell)rustc --version rustc 1.42.0
After you get familiar with nix-shell -p
you can take
the next step further and learn some
Nix. To setup a more persistent environment you can also write a
simple shell.nix
file:
{ pkgs ? import <nixpkgs> {} }: pkgs.mkShell { name = "dev-shell"; buildInputs = [ pkgs.python3 pkgs.python3Packages.virtualenv pkgs.nodejs pkgs.yarn ]; }
Then enter development environment with:
nix-shell (nix-shell)virtualenv --version 16.7.9 (nix-shell)yarn --version 1.22.4
Commit the above shell.nix
file and let you coworkers have
easier time setting their development environment.
Declarative way to build minimal docker images. No build tools inside docker image, no complex multi stage build process, only what your application needs.
The following Nix expression (default.nix
) defines a
docker image with only Python 3 installed in it.
{ pkgs ? import <nixpkgs> {} }: pkgs.dockerTools.buildLayeredImage { name = "only-hello"; contents = [ pkgs.hello ]; }
To build and run the image you need to:
nix-build ... /nix/store/…-docker-image-only-hello.tar.gz docker load -i ./result ... Loaded image: only-hello:fgzj21lg3hjv8bxlaabcsfjkh4fg5ssk docker run only-hello hello" Hello World
Learn more how to build docker images.
How hard would it be to build and configure a Amazon EC2 image?
With the following amazon.nix
we defined nginx which is
serving example /var/www
folder, having a valid ssl
certificate (via LetsEncrypt) and enabled recommended security
settings.
{ pkgs, ...}: { security.acme.acceptTerms = true; security.acme.email = "nix@example.com"; services.nginx = { enable = true; recommendedGzipSettings = true; recommendedOptimisation = true; recommendedProxySettings = true; recommendedTlsSettings = true; virtualHosts."example.com" = { enableACME = true; forceSSL = true; locations."/".root = "/var/www"; }; }; }
Now we just need to build it.
nix-build '<nixpkgs/nixos/release.nix>' \ -A amazonImage.x86_64-linux \ --arg configuration ./amazon.nix \ -o ./result ... ls ./result/ nixos-amazon-image-20.09pre130979.gfedcba-x86_64-linux.vhd nix-support