How-to install Postgresql with pgagent

Hello,
I need to install Postgresql with Pgagent extension. I have not found any documentation on this. I have found a solution that works for me, posting it here as a reminder.

Any feedback is very welcome because I am new to Nix and NixOS, and this is my first attempt at extending Nixpkgs.

I have used home-manager because I only need Postgresql for development purposes, so I start it with my user.

I’m on NixOS with nixos-unstable channel and home-manager from master.

Here is my ~/.config/nixpkgs/home.nix:

{ config, pkgs, ... }:
let
  pgagent = pkgs.stdenv.mkDerivation rec {
    name = "pgagent";
    nativeBuildInputs = [ pkgs.cmake ];
    buildInputs = [ pkgs.postgresql pkgs.boost ];

    src = pkgs.fetchFromGitHub {
      owner  = "postgres";
      repo   = name;
      rev    = "REL-4_2_2";
      sha256 = "sha256-Eoy57VMKtKQd9CyFuVLzimQPlHB4trIg2DLFeBI1w3M=";
    };

    installPhase = ''
      mkdir -p $out/bin
      cp pgagent $out/bin
      mkdir -p $out/share/postgresql/extension
      cp *.{sql,control} $out/share/postgresql/extension
    '';
  };
in
{
  home.packages = with pkgs; [
    (postgresql.withPackages(p: [ pgagent ]))
    pgagent # This makes the binary available
  ];
  # ...
  home.stateVersion = "21.05";
}

Thanks,
Marcello