$ # Hi! $ # Wondering how to start using Nix? $ # Here are a few examples: $ node -e "console.log(1+1)" node: command not found $ # Interesting, no node on this machine $ # No problem with Nix! $ nix-shell -p nodejs (nix-shell) $ node -e "console.log(1+1)"2 (nix-shell) $ # And now our environment has node. (nix-shell) $ # Without cluttering the user environment! :) $ # Typing "nix-shell -p ..." each time can be tedious. We can do better. $ # We can write everything down in shell.nix $ cat -n shell.nix 1 { pkgs ? import <nixpkgs> {} # here we import the nixpkgs package set 2 }: 3 pkgs.mkShell { # mkShell is a helper function 4 name="dev-environment"; # that requires a name 5 buildInputs = [ # and a list of packages 6 pkgs.nodejs 7 ]; 8 shellHook = '' # bash to run when you enter the shell 9 echo "Start developing..." 10 ''; 11 } $ # Pause the video to understand the shell.nix $ # To enter dev-environment simply run: $ nix-shell Start developing... (nix-shell) $ node -e "console.log(1+1)"2 (nix-shell) $ # Now go ahead commit shell.nix to your repository (nix-shell) $ # and share your development environment with your coworkers $ # For the last example, let us build a minimal docker image with Nix $ cat -n docker-redis.nix 1 { pkgs ? import <nixpkgs> { system = "x86_64-linux";} 2 }: # nixpkgs package set 3 pkgs.dockerTools.buildLayeredImage { # helper to build docker image 4 name = "nix-redis"; # give docker image a name 5 tag = "latest"; # provide a tag 6 contents = [ pkgs.redis ]; # packages in docker image 7 } $ # Pause the video and take the time to understand docker-redis.nix file $ # Now let's build the docker image and load it into docker $ nix-build docker-redis.nix -o ./result ... SKIPPING OUTPUT ... /nix/store/1crapx24sjgqm2j1wmq17k6f6a9wy66d-docker-image-nix-redis.tar.gz $ docker load -i ./result Loaded image: nix-redis:latest $ docker images | grep redis debian-redis latest 8366943c77e8 3 days ago 136MB alpine-redis latest aae644cd3417 3 days ago 6.99MB nix-redis latest 30486183a209 50 years ago 45.4MB $ # The size of our docker image is somewhere between a Debian and $ # an Alpine image $ # The redis packaged in nixpkgs is not optimized for small size $ # Let us fix this! $ cat -n redis-minimal.nix 1 { pkgs ? import <nixpkgs> {} 2 }: 3 pkgs.redis.overrideAttrs (old: { 4 # no need for systemd support in our docker image 5 makeFlags = old.makeFlags ++ ["USE_SYSTEMD=no"]; 6 # build static binary with musl 7 preBuild = '' 8 makeFlagsArray=(PREFIX="$out" 9 CC="${pkgs.musl.dev}/bin/musl-gcc -static" 10 CFLAGS="-I${pkgs.musl.dev}/include" 11 LDFLAGS="-L${pkgs.musl.dev}/lib") 12 ''; 13 # Let's remove some binaries which we don't need 14 postInstall = "rm -f $out/bin/redis-{benchmark,check-*,cli}"; 15 }) $ # In redis-minimal.nix we override the default redis build with three changes: $ # 1.) Remove the redis systemd support $ # 2.) Build a statically linked binary with musl $ # 3.) Remove all binaries apart from redis-server $ # Now let's build the docker image with our newly created minimal redis $ cat -n docker-redis-minimal.nix 1 { pkgs ? import <nixpkgs> { system = "x86_64-linux";} 2 }: # nixpkgs package set 3 let 4 redisMinimal = import ./redis-minimal.nix { inherit pkgs; }; 5 in 6 pkgs.dockerTools.buildLayeredImage { # helper to build docker image 7 name = "nix-redis-minimal"; # give docker image a name 8 tag = "latest"; # provide a tag 9 contents = [ redisMinimal ]; # use redisMinimal package 10 } $ # Let's build the new docker image now $ nix-build docker-redis-minimal.nix -o ./result ... SKIPPING OUTPUT ... /nix/store/83zcgs5xvzrgx09iv8s82wkabl8xkr03-docker-image-nix-redis-minimal.tar.gz $ docker load -i ./result Loaded image: nix-redis-minimal:latest $ docker images | grep redis debian-redis latest 8366943c77e8 3 days ago 136MB alpine-redis latest aae644cd3417 3 days ago 6.99MB nix-redis latest 30486183a209 50 years ago 45.4MB nix-redis-minimal latest a21238890680 50 years ago 2.02MB $ # Did we just produce a docker image _smaller_ than Alpine? Interesting! $ # Go tell your friends :) $ # This was a quick taste of what Nix can do. $ # I hope we made you eager to try it for yourself. $ # Happy Nixing!