Skip to content

Commit

Permalink
use cat -n instead of bat
Browse files Browse the repository at this point in the history
  • Loading branch information
garbas committed Sep 24, 2020
1 parent 8f8b26a commit 9a2566b
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 137 deletions.
110 changes: 47 additions & 63 deletions demos/cover.scenario
Expand Up @@ -29,22 +29,18 @@ $ nix-shell -p nodejs

$ # Typing "nix-shell -p ..." each time can be tedious. We can do better.
$ # We can write everything down in shell.nix
$ bat shell.nix
────────┬────────────────────────────────────────────────────────────────────
│ File: 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 │ }
────────┴────────────────────────────────────────────────────────────────────
$ 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
Expand All @@ -57,18 +53,14 @@ Start developing...
--

$ # For the last example, let us build a minimal docker image with Nix
$ bat docker-redis.nix
───────┬─────────────────────────────────────────────────────────────────────
│ File: docker-redis.nix
───────┼─────────────────────────────────────────────────────────────────────
1 │ { pkgs ? import <nixpkgs> {} # nixpkgs package set
2 │ }:
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 │ }
───────┴─────────────────────────────────────────────────────────────────────
$ cat -n docker-redis.nix
1 { pkgs ? import <nixpkgs> {} # nixpkgs package set
2 }:
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
Expand All @@ -84,46 +76,38 @@ $ # 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!
$ bat redis-minimal.nix
───────┬─────────────────────────────────────────────────────────────────────
│ File: 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 │ })
───────┴─────────────────────────────────────────────────────────────────────
$ 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
$ bat docker-redis-minimal.nix
───────┬─────────────────────────────────────────────────────────────────────
│ File: redis-minimal.nix
───────┼─────────────────────────────────────────────────────────────────────
1 │ { pkgs ? import <nixpkgs> {}
2 │ }:
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 │ }
───────┴─────────────────────────────────────────────────────────────────────
$ cat -n docker-redis-minimal.nix
1 { pkgs ? import <nixpkgs> {}
2 }:
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 ...
Expand Down
36 changes: 15 additions & 21 deletions demos/example_3.scenario
Expand Up @@ -2,26 +2,21 @@

$ # You can also persist your development environment.
$ # Here is a short example with python and nodejs:
$ bat shell.nix
────────┬────────────────────────────────────────────────────────────────────
│ File: 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.python3
7 │ pkgs.python3Packages.virtualenv
8 │ pkgs.nodejs
9 │ pkgs.yarn
10 │ ];
11 │ shellHook = '' # bash to run when you enter the shell
12 │ echo "Start developing..."
13 │ '';
14 │ }
────────┴────────────────────────────────────────────────────────────────────

$ 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.python3
7 pkgs.python3Packages.virtualenv
8 pkgs.nodejs
9 pkgs.yarn
10 ];
11 shellHook = '' # bash to run when you enter the shell
12 echo "Start developing..."
13 '';
14 }
$ # Pause the video to read and understand the shell.nix
$ # To enter the dev-environment simply run:
$ nix-shell
Expand All @@ -41,4 +36,3 @@ v10.20.1
(nix-shell) $ exit
$ # How hard is it in your company to share the same version of required
$ # tooling across different machines?

20 changes: 8 additions & 12 deletions demos/example_4.scenario
Expand Up @@ -3,18 +3,14 @@
$ # We all love docker. But over time it can become tedious to write
$ # reliable docker files.
$ # What if you could use the power of Nix to build Docker images?
$ bat docker.nix
───────┬─────────────────────────────────────────────────────────────────────
│ File: docker.nix
───────┼─────────────────────────────────────────────────────────────────────
1 │ { pkgs ? import <nixpkgs> {} # nixpkgs package set
2 │ }:
3 │ pkgs.dockerTools.buildLayeredImage { # helper to build Docker image
4 │ name = "nix-hello"; # give docker image a name
5 │ tag = "latest"; # provide a tag
6 │ contents = [ pkgs.hello ]; # packages in docker image
7 │ }
───────┴─────────────────────────────────────────────────────────────────────
$ cat -n docker.nix
1 { pkgs ? import <nixpkgs> {} # nixpkgs package set
2 }:
3 pkgs.dockerTools.buildLayeredImage { # helper to build Docker image
4 name = "nix-hello"; # give docker image a name
5 tag = "latest"; # provide a tag
6 contents = [ pkgs.hello ]; # packages in docker image
7 }
$ # Pause the video to read and understand the docker.nix
$ # Now we build a Docker image with nix-build
$ nix-build docker.nix -o result
Expand Down
40 changes: 18 additions & 22 deletions demos/example_5.scenario
Expand Up @@ -3,28 +3,24 @@
$ # How hard would it be to build and configure an Amazon EC2 image?
$ # Let us configure a Nginx to serve a "Welcome to nginx!" page, with a
$ # valid SSL certificate (via LetsEncrypt) and recommended security settings
$ bat amazon.nix
────────┬─────────────────────────────────────────────────────────────────────
│ File: amazon.nix
────────┼─────────────────────────────────────────────────────────────────────
1 │ { pkgs, ...}:
2 │ {
3 │ security.acme.acceptTerms = true;
4 │ security.acme.email = "nix@example.com";
5 │ services.nginx = {
6 │ enable = true;
7 │ recommendedGzipSettings = true;
8 │ recommendedOptimisation = true;
9 │ recommendedProxySettings = true;
10 │ recommendedTlsSettings = true;
11 │ virtualHosts."example.com" = {
12 │ enableACME = true;
13 │ forceSSL = true;
14 │ locations."/".root = "${pkgs.nginx}/html";
15 │ };
16 │ };
17 │ }
────────┴────────────────────────────────────────────────────────────────────
$ cat -n amazon.nix
1 { pkgs, ...}:
2 {
3 security.acme.acceptTerms = true;
4 security.acme.email = "nix@example.com";
5 services.nginx = {
6 enable = true;
7 recommendedGzipSettings = true;
8 recommendedOptimisation = true;
9 recommendedProxySettings = true;
10 recommendedTlsSettings = true;
11 virtualHosts."example.com" = {
12 enableACME = true;
13 forceSSL = true;
14 locations."/".root = "${pkgs.nginx}/html";
15 };
16 };
17 }
$ # Pause the video to understand the default.nix
$ # Now we just need to build it.
$ nix-build '<nixpkgs/nixos/release.nix>' \
Expand Down
34 changes: 15 additions & 19 deletions demos/example_6.scenario
Expand Up @@ -4,25 +4,21 @@ $ # In this example we will look into how to test your NixOS configuration
$ # We will create a simple configuration with the `hello` package installed
$ # system wide and check that the `hello` world binary works.

$ bat test.nix
────────┬─────────────────────────────────────────────────────────────────────
│ File: test.nix
────────┼─────────────────────────────────────────────────────────────────────
1 │ { pkgs ? import <nixpkgs> {}
2 │ }:
3 │ pkgs.nixosTest ({ lib, pkgs, ...}: {
4 │ name = "example-test";
5 │ # virtual machine with one package installed system wide
6 │ machine = { pkgs, ... }: {
7 │ environment.systemPackages = [ pkgs.hello ];
8 │ };
9 │ testScript = ''
10 │ # run hello on machine and check for output
11 │ machine.succeed('hello | grep "Hello, world!"')
12 │ # test is a simple python script
13 │ '';
14 │ })
────────┴────────────────────────────────────────────────────────────────────
$ cat -n test.nix
1 { pkgs ? import <nixpkgs> {}
2 }:
3 pkgs.nixosTest ({ lib, pkgs, ...}: {
4 name = "example-test";
5 # virtual machine with one package installed system wide
6 machine = { pkgs, ... }: {
7 environment.systemPackages = [ pkgs.hello ];
8 };
9 testScript = ''
10 # run hello on machine and check for output
11 machine.succeed('hello | grep "Hello, world!"')
12 # test is a simple python script
13 '';
14 })
$ # Pause the video to understand the test.nix

$ # To run the test you simply need to run:
Expand Down

0 comments on commit 9a2566b

Please sign in to comment.