{
  lib,
  config,
  pkgs,
  ...
}: let
  cfg = config.myConfig.rebuild;

  # script for rebuilding nixos
  rebuild = pkgs.writeShellScriptBin "rebuild" ''
    # A rebuild script that commits on a successful build
    set -e

    # cd to your config dir
    pushd ~/dotfiles/nixos

    # Edit your config
    $EDITOR

    # Autoformat your nix files
    ${pkgs.alejandra}/bin/alejandra . #&>/dev/null

    # Shows your changes
    ${pkgs.git}/bin/git diff -U0 *.nix

    echo "NixOS Rebuilding..."

    # echo using sudo so we get feedback after unlocking
    sudo echo "Beginning rebuild"
    # Rebuild, output simplified errors, log trackebacks
    # sudo nixos-rebuild switch &>nixos-switch.log || (${pkgs.coreutils}/bin/cat nixos-switch.log | ${pkgs.gnugrep}/bin/grep --color error && false)
    sudo nixos-rebuild switch

    # Get current generation metadata
    current=$(nixos-rebuild list-generations | ${pkgs.gnugrep}/bin/grep current)

    # Commit all changes with the gereation metadata
    ${pkgs.git}/bin/git commit -am "$current"

    # Back to where you were
    popd

    # Notify all OK!
    ${pkgs.libnotify}/bin/notify-send -e "NixOS Rebuilt OK!" --icon=software-update-available
  '';
in {
  options.myConfig.rebuild = {
    enable = lib.mkEnableOption "rebuild script with automatic git commits";
  };

  config = lib.mkIf cfg.enable {
    environment.systemPackages = [rebuild];
  };
}
