~afontaine/home unlisted

home/flake.nix -rw-r--r-- 9.4 KiB
f8f45072Andrew Fontaine Use a URL to Connect to the DB a month ago
                                                                                
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
{
  description = "Home, home on the web";
  inputs.nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
  inputs.flake-utils.url = "github:numtide/flake-utils";

  outputs = { self, nixpkgs, flake-utils, }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = nixpkgs.legacyPackages.${system};
        erlangPackages =
          pkgs.beam.packagesWith pkgs.beam.interpreters.erlangR23;
        node2nix = pkgs.nodePackages.node2nix;
        elixir = erlangPackages.elixir_1_11;
        erlang = erlangPackages.erlang;
        nodejs = pkgs.nodejs-14_x;
        elixir-ls = let
          version = "v0.6.5";
          pname = "elixir-ls";
          src = pkgs.fetchFromGitHub {
            owner = "elixir-lsp";
            repo = pname;
            rev = version;
            sha256 = "sha256-3M0M/L2m9P9YYngCHcQBhnRPgr23Esj1Foyl2kQDJ2Y=";
          };
        in erlangPackages.mixRelease {
          inherit version pname src;
          installPhase = ''
            runHook preInstall
            mkdir -p $out/bin
            mix do compile --no-deps-check, elixir_ls.release -o "$out/bin"
            mv "$out/bin/language_server.sh" "$out/bin/elixir-ls"
            touch $out/bin/elixir-ls.bat
            runHook postInstall
          '';
          mixDeps = erlangPackages.fetchMixDeps {
            inherit src version;
            pname = "${pname}-deps";
            sha256 = "fXdo/hKEMJVqZGbKgP8bcCsmlQT/4lO9YM5AdQo7G08=";
          };
        };
        homeWebAssetDependencies = (pkgs.callPackage ./apps/home_web/assets {
          inherit pkgs system nodejs;
        }).shell.nodeDependencies;
      in {
        devShell = pkgs.mkShell {
          NODE_ENV = "development";
          buildInputs = [
            pkgs.nodePackages.npm
            pkgs.nodePackages.prettier
            elixir
            nodejs
            pkgs.git
            pkgs.postgresql
            pkgs.nodePackages.eslint
            pkgs.cacert
            elixir-ls
            node2nix
          ] ++ pkgs.lib.optional pkgs.stdenv.isLinux pkgs.inotifyTools;
          ERL_INCLUDE_PATH = "${erlang}/lib/erlang/usr/include";
          shellHook = ''
            export PGDATA=$PWD/.postgres_data
            export PGHOST=$PWD/.postgres
            export LOG_PATH=$PGHOST/LOG
            export PGDATABASE=postgres
            export DATABASE_URL="postgresql:///postgres?host=$PGHOST"
            if [ ! -d $PGHOST ]; then
              mkdir -p $PGHOST
            fi
            if [ ! -d $PGDATA ]; then
              echo 'Initializing postgresql database...'
              initdb $PGDATA --auth=trust --no-locale --encoding=UTF-8 >/dev/null
            fi
            start() {
              pg_ctl start -l $LOG_PATH -o "-c listen_addresses= -c unix_socket_directories=$PGHOST"
            }
            ln -s ${homeWebAssetDependencies}/lib/node_modules ./apps/home_web/assets
          '';
        };
        packages = let
          version = "0.1.3";
          src = ./.;
        in flake-utils.lib.flattenTree {
          home-elixir-deps = erlangPackages.fetchMixDeps {
            inherit version src;
            pname = "home-elixir-deps";
            sha256 = "JpCXWHTRoXUsb51IPzFLhpE3uVZuZBs4IANN8VmwgQs=";
          };
          home-web-fe-deps = homeWebAssetDependencies;
          home-web-fe = let
            deps = self.packages.${system}.home-web-fe-deps;
            mixDeps = self.packages.${system}.home-elixir-deps;
          in pkgs.stdenvNoCC.mkDerivation {
            pname = "home-fe";
            nativeBuildInputs = [ nodejs ];
            inherit version src;
            configurePhase = ''
              export NODE_ENV=production
              export TAILWIND_DISABLE_TOUCH=true
            '';

            buildPhase = ''
              cp -r . $TEMPDIR
              ln -s ${deps}/lib/node_modules $TEMPDIR/apps/home_web/assets
              export PATH="${deps}/bin:$PATH"
              ${nodejs}/bin/npm run deploy --prefix $TEMPDIR/apps/home_web/assets
            '';

            installPhase = ''
              cp -r $TEMPDIR/apps/home_web/priv/static $out/
            '';
            outputHashAlgo = "sha256";
            outputHashMode = "recursive";
            outputHash = "VK9eS5MZXBVbyxylmuXq0qKKiDmIPiTi5CperUfN4YA=";
          };
          home = let
            homeWebAssetDependencies =
              (pkgs.callPackage ./apps/home_web/assets {
                inherit pkgs system nodejs;
              }).shell.nodeDependencies;
            home-web-fe = self.packages.${system}.home-web-fe;
            mixDeps = self.packages.${system}.home-elixir-deps;
          in erlangPackages.mixRelease {
            inherit version src mixDeps;
            pname = "home";
            preInstall = ''
              cp --no-preserve=mode,ownership,timestamps -R ${home-web-fe} ./apps/home_web/priv/static
              ${elixir}/bin/mix phx.digest
            '';
          };
        };
        defaultPackage = self.packages.${system}.home;
      }) // {
        nixosModules.home = { config, lib, pkgs, ... }:
          let
            cfg = config.services.home-web;
            system = config.nixpkgs.system;
            home = self.defaultPackage.${system};
          in {
            options = {
              services.home-web = {
                enable = lib.mkEnableOption "home-web";
                port = lib.mkOption {
                  type = lib.types.int;
                  default = 4000;
                  description = "Port to listen on, 4000 by default";
                };
                poolSize = lib.mkOption {
                  type = lib.types.int;
                  default = 10;
                  description = "Connection pool size, 10 by default";
                };
                secretKeyBaseFile = lib.mkOption {
                  type = lib.types.path;
                  description =
                    "A file contianing the Phoenix Secret Key Base. This should be secret, and not kept in the nix store";
                };
                dataDir = lib.mkOption {
                  type = lib.types.path;
                  default = "/var/lib/home-web";
                  description = "The directory to write data to";
                };
                databaseUrlFile = lib.mkOption {
                  type = lib.types.path;
                  description =
                    "A file containing the URL to use to connect to the database";
                };
                user = lib.mkOption {
                  type = lib.types.str;
                  default = "home-web";
                  description = "The user to run as";
                };
                group = lib.mkOption {
                  type = lib.types.str;
                  default = "home-web";
                  description = "The group the user is in";
                };
                host = lib.mkOption {
                  type = lib.types.str;
                  description =
                    "The host to configure the router generation from";
                };
              };
            };
            config = lib.mkIf cfg.enable {
              assertions = [{
                assertion = cfg.secretKeyBaseFile != "";
                message = "A base key file is necessary";
              }];

              users.users = lib.optionalAttrs (cfg.user == "home-web") {
                home-web = {
                  isSystemUser = true;
                  group = cfg.group;
                  home = cfg.dataDir;
                  createHome = true;
                };
              };

              users.groups =
                lib.optionalAttrs (cfg.group == "home-web") { home-web = { }; };

              systemd.services = {
                home-web-setup = {
                  description = "Create and Migrate Home Web Database";
                  wantedBy = [ "multi-user.target" ];
                  script = ''
                    ${home}/bin/home eval "Blog.Release.create"
                    ${home}/bin/home eval "Blog.Release.migrate"
                  '';
                  serviceConfig = {
                    Type = "oneshot";
                    User = cfg.user;
                    Group = cfg.group;
                  };
                  environment = {
                    RELEASE_TMP = cfg.dataDir;
                    TZ_DATA_DIR = cfg.dataDir;
                    SECRET_KEY_BASE_FILE = cfg.secretKeyBaseFile;
                    DATABASE_URL_FILE = cfg.databaseUrlFile;
                    HOSTNAME = cfg.host;
                  };
                };

                home-web = {
                  description = "Home, home on the web";
                  wantedBy = [ "multi-user.target" ];
                  after = [ "network.target" "home-web-setup.service" ];
                  wants = [ "home-web-setup.service" ];
                  script = ''
                    ${home}/bin/home start
                  '';
                  serviceConfig = {
                    User = cfg.user;
                    Group = cfg.group;
                  };
                  environment = {
                    RELEASE_TMP = cfg.dataDir;
                    TZ_DATA_DIR = cfg.dataDir;
                    POOL_SIZE = builtins.toString cfg.poolSize;
                    SECRET_KEY_BASE_FILE = cfg.secretKeyBaseFile;
                    PORT = builtins.toString cfg.port;
                    DATABASE_URL_FILE = cfg.databaseUrlFile;
                    HOSTNAME = cfg.host;
                  };
                };
              };
            };
          };
        nixosModule = self.nixosModules.home;
      };
}