diff --git a/README.md b/README.md index 18f8bd20..010709dc 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,21 @@ Visit https://www.pgschema.com/installation > [!NOTE] > Windows is not supported. Please use WSL (Windows Subsystem for Linux) or a Linux VM. +### Nix (flake) + +```bash +nix build +nix run +``` + +To pass arguments, use `--`: + +```bash +nix run . -- plan +``` + +If the build fails with a `vendorHash` mismatch, update `nix/pgschema.nix` with the hash printed by Nix. + ## Getting help - [Docs](https://www.pgschema.com) diff --git a/default.nix b/default.nix new file mode 100644 index 00000000..1c095b3b --- /dev/null +++ b/default.nix @@ -0,0 +1,3 @@ +{ pkgs ? import {} }: + +pkgs.callPackage ./nix/pgschema.nix {} diff --git a/flake.lock b/flake.lock new file mode 100644 index 00000000..d33b4d33 --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1774386573, + "narHash": "sha256-4hAV26quOxdC6iyG7kYaZcM3VOskcPUrdCQd/nx8obc=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "46db2e09e1d3f113a13c0d7b81e2f221c63b8ce9", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 00000000..ec7d71e8 --- /dev/null +++ b/flake.nix @@ -0,0 +1,39 @@ +{ + description = "pgschema"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05"; + }; + + outputs = { self, nixpkgs }: + let + systems = [ + "x86_64-linux" + "aarch64-linux" + "x86_64-darwin" + "aarch64-darwin" + ]; + forAllSystems = f: nixpkgs.lib.genAttrs systems (system: f system); + in + { + packages = forAllSystems (system: + let + pkgs = import nixpkgs { inherit system; }; + pgschema = pkgs.callPackage ./nix/pgschema.nix { + rev = self.shortRev or "dirty"; + buildDate = self.lastModifiedDate or "unknown"; + }; + in + { + inherit pgschema; + default = pgschema; + }); + + apps = forAllSystems (system: { + default = { + type = "app"; + program = "${self.packages.${system}.pgschema}/bin/pgschema"; + }; + }); + }; +} diff --git a/nix/pgschema.nix b/nix/pgschema.nix new file mode 100644 index 00000000..918566a8 --- /dev/null +++ b/nix/pgschema.nix @@ -0,0 +1,39 @@ +{ pkgs ? import {}, rev ? "unknown", buildDate ? "unknown" }: + +let + lib = pkgs.lib; + version = lib.strings.removeSuffix "\n" (builtins.readFile ../internal/version/VERSION); +in +pkgs.buildGoModule { + pname = "pgschema"; + inherit version; + + src = lib.cleanSource ../.; + # Prefer Go 1.24 when available; fall back to the closest newer toolchain. + go = if pkgs ? go_1_24 then pkgs.go_1_24 else pkgs.go_1_25; + subPackages = [ "." ]; + proxyVendor = true; + + # Update if `nix build` reports a vendorHash mismatch. + vendorHash = "sha256-3nV7AEsWyEvIbxHetoEsA8PPXJ6ENvU/sz7Wn5aysss="; + + env = { + CGO_ENABLED = "0"; + }; + ldflags = [ + "-s" + "-w" + "-X" + "github.com/pgplex/pgschema/cmd.GitCommit=${rev}" + "-X" + "github.com/pgplex/pgschema/cmd.BuildDate=${buildDate}" + ]; + + meta = with lib; { + description = "PostgreSQL schema management tool"; + homepage = "https://www.pgschema.com"; + license = licenses.asl20; + mainProgram = "pgschema"; + platforms = platforms.unix; + }; +}