aboutsummaryrefslogtreecommitdiff
path: root/readme.md
blob: 853c3cc927a3c21ba10220a0e09e6bc31dde4c7b (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# flaky-utils

Additional Nix Flake utility functions for personal convenience (mainly
reducing boilerplate).

No promise on API stability.


## Usage

Functions documentation provided as comments below.

```nix
{
  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 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 nixpkgs system;

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

      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;
          '';
        };
      };
    })

  ]);
}
```


## Contributing

Issues and patches: email the author.


## Licence and copyright

Copyright (C) 2022 Pacien TRAN-GIRARD.

This project is distributed under the terms of European Union Public Licence
version 1.2, a copy of which is provided in `./licence.txt`.