From 4a64f2186f499d29e47e9311aa2dd74829f90900 Mon Sep 17 00:00:00 2001 From: efim Date: Sun, 8 Oct 2023 07:20:48 +0000 Subject: [PATCH] feat: nix build enabled nix build will put binary into nix store, and symlink to ./result running with ./result/bin/auth-pocketbase-attempt serve --dir=./pb_data if i understood migrations correctly, when using as a framework migrations will be generated as go files, and will be packaged into the binary --- .gitignore | 1 + auth-notes.org | 65 +++++++++++++++++++++++++++++++++++++++++++++++++- flake.lock | 34 ++++++++++++++++++++++++++ flake.nix | 55 ++++++++++++++++++++++++++++-------------- 4 files changed, 136 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 6a81226..d1137ae 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ tmp/ .direnv pb_data pages/static/public/out.css +result diff --git a/auth-notes.org b/auth-notes.org index 5aa5d0f..00bb801 100644 --- a/auth-notes.org +++ b/auth-notes.org @@ -90,9 +90,66 @@ and i guess i could also search online for tailwindcss Makefile examples and tip ** DONE package static into single binary i guess already done? -** TODO write nix build +** DONE write nix build and it should be even easier with a Makefile? + +https://ryantm.github.io/nixpkgs/stdenv/stdenv/ + +simple stdenv.mkDerivation calls generic builder which uses Makefile + +now i have a problem with using go build in a homeless-shelter + > failed to initialize build cache at /homeless-shelter/.cache/go-build: mkdir /homeless-shelter: permission denied +*** well, especially with go.mod dependencies i'd need to use buildGoModule +but +[efim@chunky:~/Documents/personal/go-learning/auth-pocketbase-attempt]$ ./result/bin/auth-pocketbase-attempt serve +2023/10/07 04:05:56 mkdir result/bin/pb_data: read-only file system + +so, i need to pass some place in tmp? this is probably pocketbase settings, hopefully as command line argument + +https://nixos.org/manual/nixpkgs/stable/#sec-language-go +https://nixos.wiki/wiki/Go + +so, if i call executable from somewhere, it looks for pb_data in current directory + +but then for some reason +[efim@chunky:~/Documents/personal/go-learning/auth-pocketbase-attempt]$ ./result/bin/auth-pocketbase-attempt serve +2023/10/08 06:37:19 mkdir result/bin/pb_data: read-only file system + +here it tries to init pb_data near the binary + +this works: +[efim@chunky:~/Documents/personal/go-learning/auth-pocketbase-attempt]$ ./result/bin/auth-pocketbase-attempt serve --dir=./pb_data + +*** oh, i don't need to specify location of migrations. +because they are static. and should be just present in the nix store + +and --dir is already built in. nice + +well, i don't see any pb_migrations in my project directory even though, +i'm creating and updating the table +maybe it's all in pb_data now? + +if now - i'll need to add something like +#+begin_src nix + postBuild = '' + cp pb_migration $out/bin/pb_migration + ''; +#+end_src + +*** so, if using as framework migrations are not automatically enabled? +https://github.com/pocketbase/pocketbase/discussions/2218 + +https://pocketbase.io/docs/go-migrations/#enable-go-migrations +The prebuilt executable enables the migrate command by default, but when you are extending PocketBase with Go you have to enable it manually +*** now `nix build` produces the binary capable to run the site +and +#+begin_src bash +./result/bin/auth-pocketbase-attempt serve --dir=./pb_data +#+end_src +is what i need for it to pick up pb_data from work directory, cool + ** TODO write nixos module +need to pass data and migration location as params ** TODO add docker image from nix *** TODO add cli for port and host ** TODO add readme and comments @@ -102,3 +159,9 @@ can it be configured on render.com? ** TODO get icons for the auth providers. surely they are accessible from the pocketbase itself? http://localhost:8090/_/images/oauth2/apple.svg yes. +** TODO figure out and enbale migrations +https://pocketbase.io/docs/go-migrations/#enable-go-migrations + +if i understood correctly, when i enable migration generation +i would be able to modify locally run instance via admin interface, +go files with migration would be generated, i'll have to import them somewhere in my main module, and then after building/packaging when i run `serve` on production the migrations would run on the production data diff --git a/flake.lock b/flake.lock index 7ecb641..0ac0140 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,23 @@ { "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1694529238, + "narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "ff7b65b44d01cf9ba6a71320833626af21126384", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, "nixpkgs": { "locked": { "lastModified": 1695806987, @@ -16,8 +34,24 @@ }, "root": { "inputs": { + "flake-utils": "flake-utils", "nixpkgs": "nixpkgs" } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } } }, "root": "root", diff --git a/flake.nix b/flake.nix index ebaeb51..0727667 100644 --- a/flake.nix +++ b/flake.nix @@ -1,23 +1,42 @@ { description = "going to look at the pocketbase apis"; + inputs.flake-utils.url = "github:numtide/flake-utils"; - outputs = { self, nixpkgs }: { - devShell.x86_64-linux = let pkgs = nixpkgs.legacyPackages.x86_64-linux; - in pkgs.mkShell { - buildInputs = [ - pkgs.go - pkgs.wgo # for restart of project - pkgs.semgrep - pkgs.gopls - pkgs.nodePackages.tailwindcss - pkgs.nodePackages.prettier - pkgs.gnumake - ]; + outputs = { self, nixpkgs, flake-utils }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = nixpkgs.legacyPackages.${system}; + pname = "auth-pocketbase-attempt"; + version = "0.0.1"; + in rec { + devShells.default = pkgs.mkShell { + buildInputs = [ + pkgs.go + pkgs.wgo # for restart of project + pkgs.semgrep + pkgs.gopls + pkgs.nodePackages.tailwindcss + pkgs.nodePackages.prettier + pkgs.gnumake + ]; - shellHook = '' - export GOPATH=$PWD/.go - export PATH=$GOPATH/bin:$PATH - ''; - }; - }; + shellHook = '' + export GOPATH=$PWD/.go + export PATH=$GOPATH/bin:$PATH + ''; + }; + packages = rec { + auth-pocketbase-attempt = pkgs.buildGoModule { + inherit pname version; + src = pkgs.nix-gitignore.gitignoreSource [ ] ./.; + vendorHash = "sha256-7B5EkrLpL+P5wipQG5a12hrvXQn/UpYAjrz/DuHmSUQ="; # set to "" when get dependencies in go.mod + + # Adding the Tailwind build step to preBuild + preBuild = '' + ${pkgs.nodePackages.tailwindcss}/bin/tailwindcss -i pages/input.css -o pages/static/public/out.css + ''; + }; + default = auth-pocketbase-attempt; + }; + }); }