aboutsummaryrefslogtreecommitdiff
path: root/example/flake.nix
blob: 17d15fc406f741001b4c5b91612c6e531e9bc30e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
{
  description = "Example of a Flake using flaky-utils.";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.05";
    flake-utils.url = "github:numtide/flake-utils";
    flaky-utils.url = "git+https://cgit.pacien.net/libs/flaky-utils";
  };

  outputs = { self, nixpkgs, flake-utils, flaky-utils }:
  flake-utils.lib.eachDefaultSystem (system: let
    pkgs = import nixpkgs { inherit system; };
  in pkgs.lib.fold pkgs.lib.recursiveUpdate { } [

    {
      # Convenience development shell providing some tools.
      #
      # The binaries made available and the environment variable set are
      # printed when entering the shell.
      #
      # The user's default shell is used instead of Bash (sacrifying a bit of
      # reproducibility for convenience).
      #
      devShell = flaky-utils.lib.mkDevShell {
        inherit pkgs;

        tools = with pkgs; [
          postgresql_14
          pgcli
        ];

        envVars = rec {
          PGDATA = "$PWD/development_database/pgdata";
          PGHOST = "$PWD/development_database";
          PGPORT = "5432";
          PGDATABASE = "app";
          DATABASE_URL = "postgresql:///${PGDATABASE}?host=${PGHOST}";
        };

        shell = null;
      };
    }

    # Convenience isolated environment using a QEMU virtual machine.
    #
    # This defines a triplet of nixosConfiguration, package and app with the
    # given name, so that the virtual machine can be launched using
    # a command like `nix run .#sandbox`.
    #
    # By default, the VM is launched in the current console without a graphical
    # interface, dropping to a shell for the default dummy user within.
    #
    # The binaries made available through the `tools` option and the
    # environment variable set through the `envVars` option are printed when
    # entering the shell.
    #
    # The current working directory from which the Flake is run is mounted and
    # made available within the virtual machine in /mnt. The root filesystem
    # is ephemeral (written to a temporary file in /tmp).
    #
    # The virtual machine's network is isolated by default: it cannot access
    # the Internet nor the host's local network. Ports may nevertheless be
    # forwarded explicitly from host to guest and vice-versa.
    #
    (flaky-utils.lib.mkSandboxSystem {
      inherit pkgs;

      name = "sandbox";
      user = "dummy";

      tools = with pkgs; [
        postgresql
        pgcli
      ];

      envVars = {
        PGDATABASE = "app";
      };

      config = {
        virtualisation.forwardPorts = [
          { from = "host"; host.port = 5432; guest.port = 5432; }  # postgres
        ];

        services.postgresql = {
          enable = true;
          enableTCPIP = true;

          authentication = ''
            host all all 0.0.0.0/0 trust
          '';

          initialScript = pkgs.writeText "init.sql" ''
            create role dummy login superuser;
          '';
        };
      };
    })

  ]);
}