diff --git a/.gitignore b/.gitignore index b83d222..d09ffe4 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target/ +/.db/ diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index 43b293b..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,15 +0,0 @@ -version: "3" - -services: - database: - image: "postgres:latest" - environment: - POSTGRES_DB: postgres - POSTGRES_PASSWORD: postgres - ports: - - 5432:5432 - volumes: - - db_data:/var/lib/postgresql/data/ - -volumes: - db_data: diff --git a/flake.lock b/flake.lock index 1c841f4..2d843df 100644 --- a/flake.lock +++ b/flake.lock @@ -5,11 +5,11 @@ "systems": "systems" }, "locked": { - "lastModified": 1681202837, - "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=", + "lastModified": 1694529238, + "narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=", "owner": "numtide", "repo": "flake-utils", - "rev": "cfacdce06f30d2b68473a46042957675eebb3401", + "rev": "ff7b65b44d01cf9ba6a71320833626af21126384", "type": "github" }, "original": { @@ -34,39 +34,28 @@ "type": "github" } }, - "nixpkgs_2": { - "locked": { - "lastModified": 1681358109, - "narHash": "sha256-eKyxW4OohHQx9Urxi7TQlFBTDWII+F+x2hklDOQPB50=", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "96ba1c52e54e74c3197f4d43026b3f3d92e83ff9", - "type": "github" - }, - "original": { - "owner": "NixOS", - "ref": "nixpkgs-unstable", - "repo": "nixpkgs", - "type": "github" - } - }, "root": { "inputs": { + "flake-utils": "flake-utils", "nixpkgs": "nixpkgs", "rust-overlay": "rust-overlay" } }, "rust-overlay": { "inputs": { - "flake-utils": "flake-utils", - "nixpkgs": "nixpkgs_2" + "flake-utils": [ + "flake-utils" + ], + "nixpkgs": [ + "nixpkgs" + ] }, "locked": { - "lastModified": 1696039808, - "narHash": "sha256-7TbAr9LskWG6ISPhUdyp6zHboT7FsFrME5QsWKybPTA=", + "lastModified": 1696126582, + "narHash": "sha256-uo4cn/d2rHPy/fpKZKFBOaVO531zs/Doxz43imrpqZM=", "owner": "oxalica", "repo": "rust-overlay", - "rev": "a4c3c904ab29e04a20d3a6da6626d66030385773", + "rev": "fc6fe50d9a4540a1111731baaa00f207301fdeb7", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index 982e1e6..6aa30e8 100644 --- a/flake.nix +++ b/flake.nix @@ -3,13 +3,17 @@ inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; - rust-overlay.url = "github:oxalica/rust-overlay"; + flake-utils.url = "github:numtide/flake-utils"; + rust-overlay = { + url = "github:oxalica/rust-overlay"; + inputs.nixpkgs.follows = "nixpkgs"; + inputs.flake-utils.follows = "flake-utils"; + }; }; - outputs = { self, nixpkgs, rust-overlay }: + outputs = { self, nixpkgs, flake-utils, rust-overlay }: let - #supportedSystems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ]; - system = "x86_64-linux"; + supportedSystems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ]; overlays = [ (final: prev: { @@ -20,24 +24,44 @@ (import rust-overlay) ]; - - pkgs = import nixpkgs { - inherit system overlays; - }; in - { - devShells.${system}.default = with pkgs; mkShell { - packages = [ - elixir - elixir-ls - protobuf - protoc-gen-elixir - rust-bin.stable.latest.default - ] ++ lib.optionals stdenv.isLinux [ inotify-tools ]; + flake-utils.lib.eachSystem supportedSystems (system: + let + pkgs = import nixpkgs { inherit system overlays; }; + in + { + devShells.default = with pkgs; mkShell { + packages = [ + elixir + elixir-ls + protobuf + protoc-gen-elixir + rust-bin.stable.latest.default + postgresql + ] ++ lib.optionals stdenv.isLinux [ inotify-tools ]; - inputsFrom = [ - protoc-gen-elixir - ]; - }; - }; + inputsFrom = [ + protoc-gen-elixir + ]; + + shellHook = '' + export PGDATA=$PWD/.db + export PGHOST=/tmp + export DB_LOG=$PWD/.db/log + + if [ ! -d $PGDATA ]; then + initdb $PGDATA --auth=trust + fi + + if ! pg_ctl status >/dev/null; then + pg_ctl start -l $DB_LOG -o "-c unix_socket_directories=$PGHOST" + fi + + user_exists=$(psql --csv -t -d postgres -c "SELECT count(*) FROM pg_user WHERE usename='postgres'") + if [ $user_exists != "1" ]; then + createuser -s -h $PGHOST postgres + fi + ''; + }; + }); }