nix: simpler refactor into files, add scripts
This commit is contained in:
		
							parent
							
								
									082e5bf210
								
							
						
					
					
						commit
						f1c784c79c
					
				
					 7 changed files with 69 additions and 53 deletions
				
			
		
							
								
								
									
										1
									
								
								.envrc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								.envrc
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1 @@
 | 
			
		|||
use flake
 | 
			
		||||
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| 
						 | 
				
			
			@ -1,2 +1,4 @@
 | 
			
		|||
/target/
 | 
			
		||||
/.db/
 | 
			
		||||
/.vagrant/
 | 
			
		||||
/.direnv/
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -51,11 +51,11 @@
 | 
			
		|||
        ]
 | 
			
		||||
      },
 | 
			
		||||
      "locked": {
 | 
			
		||||
        "lastModified": 1696126582,
 | 
			
		||||
        "narHash": "sha256-uo4cn/d2rHPy/fpKZKFBOaVO531zs/Doxz43imrpqZM=",
 | 
			
		||||
        "lastModified": 1696644659,
 | 
			
		||||
        "narHash": "sha256-l/DgT519At8HhXDQHz3+H8AjaEbrsb7Xkqgj+JNHV6k=",
 | 
			
		||||
        "owner": "oxalica",
 | 
			
		||||
        "repo": "rust-overlay",
 | 
			
		||||
        "rev": "fc6fe50d9a4540a1111731baaa00f207301fdeb7",
 | 
			
		||||
        "rev": "126829788e99c188be4eeb805f144d73d8a00f2c",
 | 
			
		||||
        "type": "github"
 | 
			
		||||
      },
 | 
			
		||||
      "original": {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										43
									
								
								flake.nix
									
									
									
									
									
								
							
							
						
						
									
										43
									
								
								flake.nix
									
									
									
									
									
								
							| 
						 | 
				
			
			@ -11,56 +11,25 @@
 | 
			
		|||
    };
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  outputs = { self, nixpkgs, flake-utils, rust-overlay }:
 | 
			
		||||
    let
 | 
			
		||||
      supportedSystems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
 | 
			
		||||
 | 
			
		||||
      overlays = [
 | 
			
		||||
        (final: prev: {
 | 
			
		||||
          elixir = prev.beam.packages.erlang_26.elixir_1_15;
 | 
			
		||||
          elixir-ls = prev.beam.packages.erlang_26.elixir-ls.override { elixir = final.elixir; };
 | 
			
		||||
          protoc-gen-elixir = prev.callPackage ./protoc-gen-elixir.nix { elixir = final.elixir; };
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
        (import rust-overlay)
 | 
			
		||||
      ];
 | 
			
		||||
    in
 | 
			
		||||
    flake-utils.lib.eachSystem supportedSystems (system:
 | 
			
		||||
  outputs = { self, nixpkgs, flake-utils, rust-overlay }: flake-utils.lib.eachDefaultSystem (system:
 | 
			
		||||
    let
 | 
			
		||||
      pkgs = import nixpkgs { inherit system overlays; };
 | 
			
		||||
      overlays = [ (import ./nix/overlay.nix) (import rust-overlay) ];
 | 
			
		||||
      scripts = pkgs.callPackage ./nix/scripts.nix { };
 | 
			
		||||
    in
 | 
			
		||||
    {
 | 
			
		||||
      devShells.default = with pkgs; mkShell {
 | 
			
		||||
        packages = [
 | 
			
		||||
          elixir
 | 
			
		||||
          elixir-ls
 | 
			
		||||
          rustToolchain
 | 
			
		||||
          protobuf
 | 
			
		||||
          protoc-gen-elixir
 | 
			
		||||
            rust-bin.stable.latest.default
 | 
			
		||||
            postgresql
 | 
			
		||||
          scripts.prymn_db
 | 
			
		||||
        ] ++ lib.optionals stdenv.isLinux [ inotify-tools ];
 | 
			
		||||
 | 
			
		||||
          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
 | 
			
		||||
          export PROJECT_ROOT_DIR="$PWD"
 | 
			
		||||
        '';
 | 
			
		||||
      };
 | 
			
		||||
    });
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										11
									
								
								nix/overlay.nix
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								nix/overlay.nix
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,11 @@
 | 
			
		|||
final: prev: {
 | 
			
		||||
  rustToolchain = final.rust-bin.stable.latest.default.override {
 | 
			
		||||
    targets = [ "x86_64-unknown-linux-musl" "aarch64-unknown-linux-musl" ];
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  elixir = prev.beam.packages.erlang_26.elixir_1_15;
 | 
			
		||||
 | 
			
		||||
  elixir-ls = prev.beam.packages.erlang_26.elixir-ls.override { elixir = final.elixir; };
 | 
			
		||||
 | 
			
		||||
  protoc-gen-elixir = prev.callPackage ./protoc-gen-elixir.nix { elixir = final.elixir; };
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										33
									
								
								nix/scripts.nix
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								nix/scripts.nix
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,33 @@
 | 
			
		|||
{ writeShellApplication, postgresql, ... }:
 | 
			
		||||
 | 
			
		||||
{
 | 
			
		||||
  prymn_db = writeShellApplication {
 | 
			
		||||
    name = "prymn_db";
 | 
			
		||||
 | 
			
		||||
    runtimeInputs = [ postgresql ];
 | 
			
		||||
 | 
			
		||||
    text = ''
 | 
			
		||||
      #!/usr/bin/env bash
 | 
			
		||||
 | 
			
		||||
      set -e
 | 
			
		||||
 | 
			
		||||
      export PGDATA=$PROJECT_ROOT_DIR/.db
 | 
			
		||||
      export PGHOST=/tmp
 | 
			
		||||
      export DB_LOG=$PROJECT_ROOT_DIR/.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"
 | 
			
		||||
          echo "starting your dev database..."
 | 
			
		||||
      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
 | 
			
		||||
    '';
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in a new issue