diff --git a/.envrc b/.envrc new file mode 100644 index 00000000..a41f435f --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake . --no-pure-eval diff --git a/.gitignore b/.gitignore index ceb436fe..e62a093b 100755 --- a/.gitignore +++ b/.gitignore @@ -43,11 +43,13 @@ next-env.d.ts # eslint .eslintcache +# nix dev env +!.envrc +.direnv +.devenv + # zipline uploads*/ *.crt *.key -generated - -# nix dev env -/.psql_db/ \ No newline at end of file +generated \ No newline at end of file diff --git a/README.md b/README.md index 344c51a2..33cde3a2 100755 --- a/README.md +++ b/README.md @@ -200,21 +200,29 @@ Here's how to setup Zipline for development #### Nix -If you have [Nix](https://nixos.org/) installed, you can use the provided dev shell to get started quickly. Just run: +If you have [direnv](https://direnv.net/) installed, you can simply cd into the cloned directory and run the following command: ```bash -nix develop +direnv allow ``` -This will start a postgres server, and drop you into a shell with the necessary tools installed: +Granted that you have direnv setup properly, you will now be in a new nix shell with all the dependencies and PostgreSQL installed. -- nodejs -- corepack -- git -- ffmpeg -- postgres server +If you aren't using direnv, you can run the following command to enter the nix shell: -After hopping into the dev shell, you can follow the instructions below (skipping the prerequisites) to setup a configuration and start the server. +```bash +nix develop --no-pure-eval +``` + +Useful commands regarding the postgres server: + +| Command | Description | +| --------------- | --------------------------------------------------- | +| `pgup` | Starts the postgres server in the background. | +| `pgdown` | Stops the postgres server running in the background | +| `pg_ctl status` | See if the postgres server is running | + +After familiarizing yourself with the environment, you can continue below (skipping the prerequisites since they are already installed). #### Prerequisites diff --git a/flake.lock b/flake.lock index 69cf111f..6d994fc1 100644 --- a/flake.lock +++ b/flake.lock @@ -1,20 +1,211 @@ { "nodes": { - "flake-utils": { + "cachix": { "inputs": { - "systems": "systems" + "devenv": [ + "devenv" + ], + "flake-compat": [ + "devenv" + ], + "git-hooks": [ + "devenv", + "git-hooks" + ], + "nixpkgs": [ + "devenv", + "nixpkgs" + ] }, "locked": { - "lastModified": 1731533236, - "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", - "owner": "numtide", - "repo": "flake-utils", - "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "lastModified": 1748883665, + "narHash": "sha256-R0W7uAg+BLoHjMRMQ8+oiSbTq8nkGz5RDpQ+ZfxxP3A=", + "owner": "cachix", + "repo": "cachix", + "rev": "f707778d902af4d62d8dd92c269f8e70de09acbe", "type": "github" }, "original": { - "owner": "numtide", - "repo": "flake-utils", + "owner": "cachix", + "ref": "latest", + "repo": "cachix", + "type": "github" + } + }, + "devenv": { + "inputs": { + "cachix": "cachix", + "flake-compat": "flake-compat", + "git-hooks": "git-hooks", + "nix": "nix", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1753888869, + "narHash": "sha256-VRYrrUmvXnBzfzuJVoI3os1H/0l8cJQ2KnrrxWkTB3E=", + "owner": "cachix", + "repo": "devenv", + "rev": "bdf26a4453eff6bae835f33d519a36f77e0ca257", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "devenv", + "type": "github" + } + }, + "devenv-root": { + "flake": false, + "locked": { + "narHash": "sha256-d6xi4mKdjkX2JFicDIv5niSzpyI0m/Hnm8GGAIU04kY=", + "type": "file", + "url": "file:///dev/null" + }, + "original": { + "type": "file", + "url": "file:///dev/null" + } + }, + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1747046372, + "narHash": "sha256-CIVLLkVgvHYbgI2UpXvIIBJ12HWgX+fjA8Xf8PUmqCY=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "9100a0f413b0c601e0533d1d94ffd501ce2e7885", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-parts": { + "inputs": { + "nixpkgs-lib": [ + "devenv", + "nix", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1733312601, + "narHash": "sha256-4pDvzqnegAfRkPwO3wmwBhVi/Sye1mzps0zHWYnP88c=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "205b12d8b7cd4802fbcb8e8ef6a0f1408781a4f9", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "flake-parts_2": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib" + }, + "locked": { + "lastModified": 1753121425, + "narHash": "sha256-TVcTNvOeWWk1DXljFxVRp+E0tzG1LhrVjOGGoMHuXio=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "644e0fc48951a860279da645ba77fe4a6e814c5e", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "git-hooks": { + "inputs": { + "flake-compat": [ + "devenv", + "flake-compat" + ], + "gitignore": "gitignore", + "nixpkgs": [ + "devenv", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1750779888, + "narHash": "sha256-wibppH3g/E2lxU43ZQHC5yA/7kIKLGxVEnsnVK1BtRg=", + "owner": "cachix", + "repo": "git-hooks.nix", + "rev": "16ec914f6fb6f599ce988427d9d94efddf25fe6d", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "git-hooks.nix", + "type": "github" + } + }, + "gitignore": { + "inputs": { + "nixpkgs": [ + "devenv", + "git-hooks", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1709087332, + "narHash": "sha256-HG2cCnktfHsKV0s4XW83gU3F57gaTljL9KNSuG6bnQs=", + "owner": "hercules-ci", + "repo": "gitignore.nix", + "rev": "637db329424fd7e46cf4185293b9cc8c88c95394", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "gitignore.nix", + "type": "github" + } + }, + "nix": { + "inputs": { + "flake-compat": [ + "devenv", + "flake-compat" + ], + "flake-parts": "flake-parts", + "git-hooks-nix": [ + "devenv", + "git-hooks" + ], + "nixpkgs": [ + "devenv", + "nixpkgs" + ], + "nixpkgs-23-11": [ + "devenv" + ], + "nixpkgs-regression": [ + "devenv" + ] + }, + "locked": { + "lastModified": 1752773918, + "narHash": "sha256-dOi/M6yNeuJlj88exI+7k154z+hAhFcuB8tZktiW7rg=", + "owner": "cachix", + "repo": "nix", + "rev": "031c3cf42d2e9391eee373507d8c12e0f9606779", + "type": "github" + }, + "original": { + "owner": "cachix", + "ref": "devenv-2.30", + "repo": "nix", "type": "github" } }, @@ -34,26 +225,28 @@ "type": "github" } }, - "root": { - "inputs": { - "flake-utils": "flake-utils", - "nixpkgs": "nixpkgs" - } - }, - "systems": { + "nixpkgs-lib": { "locked": { - "lastModified": 1681028828, - "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", - "owner": "nix-systems", - "repo": "default", - "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "lastModified": 1751159883, + "narHash": "sha256-urW/Ylk9FIfvXfliA1ywh75yszAbiTEVgpPeinFyVZo=", + "owner": "nix-community", + "repo": "nixpkgs.lib", + "rev": "14a40a1d7fb9afa4739275ac642ed7301a9ba1ab", "type": "github" }, "original": { - "owner": "nix-systems", - "repo": "default", + "owner": "nix-community", + "repo": "nixpkgs.lib", "type": "github" } + }, + "root": { + "inputs": { + "devenv": "devenv", + "devenv-root": "devenv-root", + "flake-parts": "flake-parts_2", + "nixpkgs": "nixpkgs" + } } }, "root": "root", diff --git a/flake.nix b/flake.nix index 1c86ad99..f898d884 100644 --- a/flake.nix +++ b/flake.nix @@ -1,84 +1,114 @@ { - description = "dev env for zipline"; - inputs = { + # required for some reason when entering the shell for devenv + devenv-root = { + url = "file+file:///dev/null"; + flake = false; + }; + # node 24.4.1, postgres 17 nixpkgs.url = "github:nixos/nixpkgs/b527e89270879aaaf584c41f26b2796be634bc9d"; - flake-utils.url = "github:numtide/flake-utils"; + flake-parts.url = "github:hercules-ci/flake-parts"; + + devenv.url = "github:cachix/devenv"; + devenv.inputs.nixpkgs.follows = "nixpkgs"; + }; + + nixConfig = { + extra-trusted-public-keys = "devenv.cachix.org-1:w1cLUi8dv3hnoSPGAuibQv+f9TZLr6cv/Hm9XgU50cw="; + extra-substituters = "https://devenv.cachix.org"; }; outputs = - { - self, - nixpkgs, - flake-utils, - }: - flake-utils.lib.eachDefaultSystem ( - system: - let - pkgs = import nixpkgs { - inherit system; + inputs@{ flake-parts, devenv-root, ... }: + flake-parts.lib.mkFlake { inherit inputs; } { + imports = [ + inputs.devenv.flakeModule + ]; + + systems = [ + "x86_64-linux" + "x86_64-darwin" + "aarch64-linux" + "aarch64-darwin" + ]; + + perSystem = + { + config, + self', + inputs', + pkgs, + system, + ... + }: + let + psqlConfig = { + username = "postgres"; + password = "postgres"; + database = "zipline"; + }; + in + { + devenv.shells.default = { + packages = with pkgs; [ + git + + # to generate thumbnails + ffmpeg + + # for testing docker + colima + docker + docker-compose + ]; + + scripts = { + pgup.exec = '' + process-compose -D + ''; + + pgdown.exec = '' + process-compose down + ''; + + # ensure that volumes are mounted with write access for docker containers + start_colima.exec = '' + colima start --mount $PWD/themes:w --mount $PWD/uploads:w --mount $PWD/public:w + ''; + }; + + enterShell = '' + export name="zipline-env"; + echo -e "\n[$name]: run 'pgup' to start services, 'pgdown' to stop services"; + ''; + + languages.javascript = { + enable = true; + package = pkgs.nodejs_24; + + corepack.enable = true; + }; + + services.postgres = { + enable = true; + package = pkgs.postgresql_17; + + initialScript = '' + CREATE ROLE "${psqlConfig.username}" WITH LOGIN PASSWORD '${psqlConfig.password}' SUPERUSER; + ''; + + initialDatabases = [ + { + name = psqlConfig.database; + user = psqlConfig.username; + } + ]; + + listen_addresses = "0.0.0.0"; + port = 5432; + }; + }; }; - - nodejs = pkgs.nodejs_24; - postgres = pkgs.postgresql; - psqlDir = ".psql_db/data"; - - psqlUsername = "postgres"; - psqlPassword = "postgres"; - in - { - devShells.default = pkgs.mkShell { - name = "zipline"; - - buildInputs = [ - nodejs - postgres - - pkgs.git - pkgs.corepack - pkgs.ffmpeg - ]; - - shellHook = '' - export PGDATA="$PWD/${psqlDir}" - export PGUSER="${psqlUsername}" - export PGPASSWORD="${psqlPassword}" - export PGPORT=5432 - - if [ ! -d "$PGDATA" ]; then - echo "Initializing PostgreSQL data directory at $PGDATA" - initdb -D "$PGDATA" --username="$PGUSER" --pwfile=<(echo "$PGPASSWORD") - fi - - # listen on localhost - echo "host all all 127.0.0.1/32 password" >> "$PGDATA/pg_hba.conf" - echo "host all all ::1/128 password" >> "$PGDATA/pg_hba.conf" - sed -i "s/^#\?listen_addresses.*/listen_addresses = 'localhost'/" "$PGDATA/postgresql.conf" - - echo "Starting PostgreSQL..." - pg_ctl -D "$PGDATA" -o "-p $PGPORT" -w start - - echo -e "PostgreSQL is ready at postgresql://$PGUSER:$PGPASSWORD@localhost:$PGPORT/postgres\n\n" - - stop_postgres() { - echo "Stopping PostgreSQL..." - pg_ctl -D "$PGDATA" stop - } - - # trap pg to stop on exiting the dev shell - trap stop_postgres EXIT - - # use zsh if zsh is available - if command -v zsh >/dev/null 2>&1; then - zsh - else - $SHELL - fi - - exit - ''; - }; - } - ); + }; }