From 77338f9cc2fa96975a51c96b78e5bd50e5991bf6 Mon Sep 17 00:00:00 2001 From: thiloho <123883702+thiloho@users.noreply.github.com> Date: Fri, 9 Aug 2024 23:18:07 +0200 Subject: [PATCH] Put nix package derivations in separate file and join symlinks --- flake.nix | 41 ++++++++----------------------------- nix/module-test.nix | 2 ++ nix/module.nix | 7 ++++--- nix/package.nix | 50 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 36 deletions(-) create mode 100644 nix/package.nix diff --git a/flake.nix b/flake.nix index 48c3797..680cf6e 100644 --- a/flake.nix +++ b/flake.nix @@ -41,27 +41,7 @@ module-test = self.nixosConfigurations.${system}.module-test.config.system.build.vm; dev-vm = self.nixosConfigurations.${system}.dev-vm.config.system.build.vm; - web = pkgs.buildNpmPackage { - name = "archtika-web-app"; - src = ./web-app; - npmDepsHash = "sha256-DmIII/x5ANlEpKtnZC/JlbVAvhbgnSiNn8hkj+qVCZY="; - npmFlags = [ "--legacy-peer-deps" ]; - installPhase = '' - mkdir $out - cp package.json $out - cp -r node_modules $out - cp -r build/* $out - ''; - }; - - api = pkgs.stdenv.mkDerivation { - name = "archtika-api"; - src = ./rest-api; - installPhase = '' - mkdir $out - cp -r db/migrations $out - ''; - }; + default = pkgs.callPackage ./nix/package.nix { }; } ); @@ -76,7 +56,9 @@ program = "${pkgs.writeShellScriptBin "api-setup" '' ${pkgs.postgresql_16}/bin/psql postgres://postgres@localhost:15432/archtika -c "ALTER DATABASE archtika SET \"app.jwt_secret\" TO 'a42kVyAhTImYxZeebZkApoAZLmf0VtDA'" - ${pkgs.dbmate}/bin/dbmate --url postgres://postgres@localhost:15432/archtika?sslmode=disable --migrations-dir ${self.packages.${system}.api}/migrations up + ${pkgs.dbmate}/bin/dbmate --url postgres://postgres@localhost:15432/archtika?sslmode=disable --migrations-dir ${ + self.packages.${system}.default + }/rest-api/db/migrations up PGRST_DB_SCHEMAS="api" PGRST_DB_ANON_ROLE="anon" PGRST_OPENAPI_MODE="ignore-privileges" PGRST_DB_URI="postgres://authenticator@localhost:15432/archtika" PGRST_JWT_SECRET="a42kVyAhTImYxZeebZkApoAZLmf0VtDA" ${pkgs.postgrest}/bin/postgrest ''}/bin/api-setup"; @@ -85,7 +67,9 @@ web = { type = "app"; program = "${pkgs.writeShellScriptBin "web-wrapper" '' - ORIGIN=http://localhost:4000 HOST=127.0.0.1 PORT=4000 ${pkgs.nodejs_22}/bin/node ${self.packages.${system}.web} + ORIGIN=http://localhost:4000 HOST=127.0.0.1 PORT=4000 ${pkgs.nodejs_22}/bin/node ${ + self.packages.${system}.default + }/web-app ''}/bin/web-wrapper"; }; } @@ -96,7 +80,7 @@ inherit system; modules = [ ./nix/module-test.nix - { _module.args.archtikaPackages = self.packages.${system}; } + { _module.args.localArchtikaPackage = self.packages.${system}.default; } ]; }; dev-vm = nixpkgs.lib.nixosSystem { @@ -105,15 +89,6 @@ }; }); - nixosModules = { - archtika = - { pkgs, ... }: - { - imports = [ ./nix/module.nix ]; - _module.args.archtikaPackages = self.packages.${pkgs.system}; - }; - }; - formatter = forAllSystems ( system: let diff --git a/nix/module-test.nix b/nix/module-test.nix index fc1fb02..6e8b7f1 100644 --- a/nix/module-test.nix +++ b/nix/module-test.nix @@ -2,6 +2,7 @@ pkgs, lib, modulesPath, + localArchtikaPackage, ... }: { @@ -42,6 +43,7 @@ services.archtika = { enable = true; + package = localArchtikaPackage; jwtSecret = "test-secret"; }; diff --git a/nix/module.nix b/nix/module.nix index 211b1a8..de81ed4 100644 --- a/nix/module.nix +++ b/nix/module.nix @@ -2,7 +2,6 @@ config, lib, pkgs, - archtikaPackages, ... }: @@ -15,6 +14,8 @@ in options.services.archtika = { enable = mkEnableOption "archtika service"; + package = mkPackageOption pkgs "archtika" { }; + user = mkOption { type = types.str; default = "archtika"; @@ -78,7 +79,7 @@ in script = '' ${pkgs.postgresql_16}/bin/psql postgres://postgres@localhost:5432/${cfg.databaseName} -c "ALTER DATABASE ${cfg.databaseName} SET \"app.jwt_secret\" TO '${cfg.jwtSecret}'" - ${pkgs.dbmate}/bin/dbmate --url postgres://postgres@localhost:5432/archtika?sslmode=disable --migrations-dir ${archtikaPackages.api}/migrations up + ${pkgs.dbmate}/bin/dbmate --url postgres://postgres@localhost:5432/archtika?sslmode=disable --migrations-dir ${cfg.package}/rest-api/db/migrations up PGRST_SERVER_PORT=${toString cfg.port} PGRST_DB_SCHEMAS="api" PGRST_DB_ANON_ROLE="anon" PGRST_OPENAPI_MODE="ignore-privileges" PGRST_DB_URI="postgres://authenticator@localhost:5432/${cfg.databaseName}" PGRST_JWT_SECRET="${cfg.jwtSecret}" ${pkgs.postgrest}/bin/postgrest ''; @@ -96,7 +97,7 @@ in }; script = '' - ORIGIN=http://localhost:${toString cfg.webAppPort} PORT=${toString cfg.webAppPort} ${pkgs.nodejs_22}/bin/node ${archtikaPackages.web} + ORIGIN=http://localhost:${toString cfg.webAppPort} PORT=${toString cfg.webAppPort} ${pkgs.nodejs_22}/bin/node ${cfg.package}/web-app ''; }; diff --git a/nix/package.nix b/nix/package.nix new file mode 100644 index 0000000..c5b8354 --- /dev/null +++ b/nix/package.nix @@ -0,0 +1,50 @@ +{ + lib, + stdenv, + buildNpmPackage, + symlinkJoin, +}: + +let + pname = "archtika"; + version = "1.0.0"; + + web = buildNpmPackage { + inherit pname version; + name = "archtika-web-app"; + src = ../web-app; + npmDepsHash = "sha256-DmIII/x5ANlEpKtnZC/JlbVAvhbgnSiNn8hkj+qVCZY="; + npmFlags = [ "--legacy-peer-deps" ]; + installPhase = '' + mkdir -p $out/web-app + cp package.json $out/web-app + cp -r node_modules $out/web-app + cp -r build/* $out/web-app + ''; + }; + + api = stdenv.mkDerivation { + inherit pname version; + name = "archtika-api"; + src = ../rest-api; + installPhase = '' + mkdir -p $out/rest-api/db/migrations + cp -r db/migrations/* $out/rest-api/db/migrations + ''; + }; +in +symlinkJoin { + name = pname; + paths = [ + web + api + ]; + + meta = with lib; { + description = "A modern, performant and lightweight CMS"; + homepage = "https://archtika.com"; + license = licenses.mit; + maintainers = with maintainers; [ thiloho ]; + platforms = platforms.unix; + }; +}