| 1 | # valley self-update on a timer, for a home-manager user, so the valley CLI |
| 2 | # follows a branch of the-valley without a system rebuild (selfupdate/). |
| 3 | # The flake offers it as homeManagerModules.valley. Enabling it adds the |
| 4 | # valley-self-update user service, which runs `valley self-update`, and a |
| 5 | # timer that starts the service two minutes after boot and every five |
| 6 | # minutes after. Each run installs the branch's tip into the user's nix |
| 7 | # profile once that exact commit builds and passes the smoke test; its |
| 8 | # history is in `journalctl --user -u valley-self-update`. |
| 9 | { |
| 10 | config, |
| 11 | lib, |
| 12 | pkgs, |
| 13 | ... |
| 14 | }: |
| 15 | let |
| 16 | cfg = config.programs.valley.selfUpdate; |
| 17 | in |
| 18 | { |
| 19 | options.programs.valley.selfUpdate = { |
| 20 | enable = lib.mkEnableOption "the valley-self-update user timer, which installs the tip of a branch of the-valley into the user's nix profile"; |
| 21 | |
| 22 | repo = lib.mkOption { |
| 23 | type = lib.types.str; |
| 24 | example = "git@classic-laddie:the-valley"; |
| 25 | description = '' |
| 26 | The repository of the-valley to follow, as `valley self-update |
| 27 | --repo` takes it: an https://, ssh://, git:// or file:// URL, an |
| 28 | absolute path, or user@host:path. user@host:path reads a valley |
| 29 | host's project from the git user's home, as git does. |
| 30 | ''; |
| 31 | }; |
| 32 | |
| 33 | branch = lib.mkOption { |
| 34 | type = lib.types.str; |
| 35 | default = "main"; |
| 36 | description = "The branch of the repository to follow."; |
| 37 | }; |
| 38 | }; |
| 39 | |
| 40 | config = lib.mkIf cfg.enable { |
| 41 | # The first valley on PATH runs it, so the newest one installed does: |
| 42 | # the one self-update put in the nix profile, else the one this |
| 43 | # profile installs. HOME is the user's, so git and ssh read the user's |
| 44 | # own config and keys, as they do in a shell. The repository and branch |
| 45 | # are passed as flags, so the timer needs no git config. |
| 46 | systemd.user.services.valley-self-update = { |
| 47 | Unit.Description = "Install the tip of the-valley's ${cfg.branch} into the nix profile"; |
| 48 | Service = { |
| 49 | Type = "oneshot"; |
| 50 | Environment = [ |
| 51 | "PATH=${ |
| 52 | lib.concatStringsSep ":" [ |
| 53 | "${config.home.homeDirectory}/.nix-profile/bin" |
| 54 | "/etc/profiles/per-user/${config.home.username}/bin" |
| 55 | (lib.makeBinPath [ |
| 56 | pkgs.nix |
| 57 | pkgs.git |
| 58 | pkgs.openssh # git's ssh, for a repository on a valley host |
| 59 | pkgs.coreutils |
| 60 | ]) |
| 61 | ] |
| 62 | }" |
| 63 | ]; |
| 64 | ExecStart = toString ( |
| 65 | pkgs.writeShellScript "valley-self-update" '' |
| 66 | exec valley self-update --repo ${lib.escapeShellArg cfg.repo} --branch ${lib.escapeShellArg cfg.branch} |
| 67 | '' |
| 68 | ); |
| 69 | }; |
| 70 | }; |
| 71 | systemd.user.timers.valley-self-update = { |
| 72 | Unit.Description = "Follow the-valley's ${cfg.branch}"; |
| 73 | Timer = { |
| 74 | OnBootSec = "2min"; |
| 75 | OnUnitActiveSec = "5min"; |
| 76 | }; |
| 77 | Install.WantedBy = [ "timers.target" ]; |
| 78 | }; |
| 79 | }; |
| 80 | } |