diff --git a/README.md b/README.md index bedbdb4..77de3ef 100644 --- a/README.md +++ b/README.md @@ -145,22 +145,24 @@ pgzx is currently under heavy development by the [Xata](https://xata.io) team. I ### Develpment shell and local installation -We use Nix to provide a local development shell. But you should be able to run -the following scripts without the development shell. +We use Nix to provide a local development shell. +This ensures that we have a stable environment with all dependencies available +in the expected versions. This is especially important with Zig, which is still +in development. + +For this purpose it is possible do use this project as input in downstream +flake files as well. + +The tools we use also require some environment variables set, which are already +pre-configured in the develpment shell. We would recommend the [nix-installer from DeterminateSystems](https://github.com/DeterminateSystems/nix-installer). The installer enables Nix Flakes (used by this project) out of the box and also provides an uninstaller. -TODO: test the scripts without development shell - -TODO: support to create develpment shell in docker container - -Still, we would recommend to use the development shell as we already provide -all compatible dependencies that you will need for developing within this -repository. -The tools we use also require some environment variables set, which are already -pre-configured in the develpment shell. +If you want to try out the project without having to install Nix on your +system, you can do so using Docker. You can build the docker image by running +the `dev/docker/build.sh` script. The docker image is names `pgzx:latest`. To enter the develpment shell run: @@ -168,9 +170,16 @@ To enter the develpment shell run: $ nix develop ``` +If you want to use the docker instead, run: + +``` +$ ./dev/docker/run.sh +``` + + NOTE: We also provide an `.envrc` file to automatically enter the development shell when entering -the projects folder. +the projects folder. If you use direnv you can enable the environment via `direnv allow`. The nix configuration already installs PostgresSQL, but for testing we want to have a local postgres installation where we can install our test extensions in. @@ -187,14 +196,14 @@ $ ls out The `out/default` folder is a symlink to the postgres installation currently in use. -Having a local installation we want to create a local database and user that we can run: +Having a local installation we want to create a local database and user: ``` $ pginit ... ``` -This did create a local database names and `postgres` user. The script allows us to configure an alternative name for the cluster, database or user. This allows us to create multiple clusters within our current installation. +This did create a local database named `postgres`. The script allows us to configure an alternative name for the cluster, database or user. This allows us to create multiple clusters within our current installation. We can start and stop the database using `pgstart` and `pgstop`. Let's test our current setup: diff --git a/dev/docker/Dockerfile b/dev/docker/Dockerfile new file mode 100644 index 0000000..f541b25 --- /dev/null +++ b/dev/docker/Dockerfile @@ -0,0 +1,41 @@ +FROM ubuntu:latest +RUN apt update -y +RUN apt install curl git sudo xz-utils -y + +# Create a non-root user +ARG USERNAME=dev +ARG USER_UID=1000 +ARG USER_GID=$USER_UID + +RUN groupadd --gid $USER_GID $USERNAME \ + && useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \ + && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \ + && chmod 0440 /etc/sudoers.d/$USERNAME \ + && mkdir /run/postgresql \ + && chown -R $USER_UID:$USER_GID /run/postgresql + +# Install Nix + +RUN mkdir -p /etc/nix && \ + mkdir -p /nix && chown $USERNAME /nix && \ + echo "experimental-features = nix-command flakes" >> /etc/nix/nix.conf + +USER $USERNAME + +RUN curl -L https://nixos.org/nix/install -o /nix/install-nix && \ + chmod a+x /nix/install-nix && \ + /nix/install-nix --no-daemon --no-channel-add && \ + rm /nix/install-nix + +ENV PATH="$PATH:/home/dev/.nix-profile/bin:/home/dev/.nix-profile/bin/nix" + +# Prebuild development environment + +RUN mkdir -p /home/dev/workdir && chown -R $USERNAME /home/dev/workdir +WORKDIR /home/dev/workdir +COPY *.nix . +COPY *.lock . +COPY nix nix +RUN nix develop -c true + +ENTRYPOINT ["nix", "develop"] diff --git a/dev/docker/build.sh b/dev/docker/build.sh new file mode 100755 index 0000000..f492a51 --- /dev/null +++ b/dev/docker/build.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +set -e +set -x + +SCRIPT_DIR=$(cd $(dirname $0); pwd) +PRJ_ROOT=${PRJ_ROOT:-$(realpath $SCRIPT_DIR/../..)} +DOCKERFILE=${DOCKERFILE:-$PRJ_ROOT/dev/docker/Dockerfile} + +cd $PRJ_ROOT +docker build -t pgzx:latest -f $DOCKERFILE . diff --git a/dev/docker/run.sh b/dev/docker/run.sh new file mode 100755 index 0000000..293fe2c --- /dev/null +++ b/dev/docker/run.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +set -e +set -x + +SCRIPT_DIR=$(cd $(dirname $0); pwd) +PRJ_ROOT=${PRJ_ROOT:-$(realpath $SCRIPT_DIR/../..)} + +docker run -i -t --rm \ + -v $PRJ_ROOT:/home/dev/workdir \ + -w /home/dev/workdir \ + pgzx:latest \ + $@