Merge pull request #330634 from r-ryantm/auto-update/circumflex
[NixPkgs.git] / pkgs / shells / zsh / oh-my-zsh / default.nix
blobab4a161c616d5cb6b8d625be8403cf009e3d11eb
1 # This script was inspired by the ArchLinux User Repository package:
3 #   https://aur.archlinux.org/cgit/aur.git/tree/PKGBUILD?h=oh-my-zsh-git
4 { lib, stdenv, fetchFromGitHub, nixosTests, writeScript, common-updater-scripts
5 , git, nix, nixfmt-classic, jq, coreutils, gnused, curl, cacert, bash }:
7 stdenv.mkDerivation rec {
8   version = "2024-07-03";
9   pname = "oh-my-zsh";
11   src = fetchFromGitHub {
12     owner = "ohmyzsh";
13     repo = "ohmyzsh";
14     rev = "057f3ec67e65661d3c01b757ec5cad0a3718453e";
15     sha256 = "sha256-gRQ1F13/CZaxLmDT5L+seUPldP7pBy3n3AcnfBnpcO8=";
16   };
18   strictDeps = true;
19   buildInputs = [ bash ];
21   installPhase = ''
22     runHook preInstall
24     outdir=$out/share/oh-my-zsh
25     template=templates/zshrc.zsh-template
27     mkdir -p $outdir
28     cp -r * $outdir
29     cd $outdir
31     rm LICENSE.txt
32     rm -rf .git*
34     chmod -R +w templates
36     # Change the path to oh-my-zsh dir and disable auto-updating.
37     sed -i -e "s#ZSH=\$HOME/.oh-my-zsh#ZSH=$outdir#" \
38            -e 's/\# \(DISABLE_AUTO_UPDATE="true"\)/\1/' \
39      $template
41     chmod +w oh-my-zsh.sh
43     # Both functions expect oh-my-zsh to be in ~/.oh-my-zsh and try to
44     # modify the directory.
45     cat >> oh-my-zsh.sh <<- EOF
47     # Undefine functions that don't work on Nix.
48     unfunction uninstall_oh_my_zsh
49     unfunction upgrade_oh_my_zsh
50     EOF
52     # Look for .zsh_variables, .zsh_aliases, and .zsh_funcs, and source
53     # them, if found.
54     cat >> $template <<- EOF
56     # Load the variables.
57     if [ -f ~/.zsh_variables ]; then
58         . ~/.zsh_variables
59     fi
61     # Load the functions.
62     if [ -f ~/.zsh_funcs ]; then
63       . ~/.zsh_funcs
64     fi
66     # Load the aliases.
67     if [ -f ~/.zsh_aliases ]; then
68         . ~/.zsh_aliases
69     fi
70     EOF
72     runHook postInstall
73   '';
75   passthru = {
76     tests = { inherit (nixosTests) oh-my-zsh; };
78     updateScript = writeScript "update.sh" ''
79       #!${stdenv.shell}
80       set -o errexit
81       PATH=${
82         lib.makeBinPath [
83           common-updater-scripts
84           curl
85           cacert
86           git
87           nixfmt-classic
88           nix
89           jq
90           coreutils
91           gnused
92         ]
93       }
95       oldVersion="$(nix-instantiate --eval -E "with import ./. {}; lib.getVersion oh-my-zsh" | tr -d '"')"
96       latestSha="$(curl -L -s https://api.github.com/repos/ohmyzsh/ohmyzsh/commits\?sha\=master\&since\=$oldVersion | jq -r '.[0].sha')"
98       if [ ! "null" = "$latestSha" ]; then
99         nixpkgs="$(git rev-parse --show-toplevel)"
100         default_nix="$nixpkgs/pkgs/shells/zsh/oh-my-zsh/default.nix"
101         latestDate="$(curl -L -s https://api.github.com/repos/ohmyzsh/ohmyzsh/commits/$latestSha | jq '.commit.committer.date' | sed 's|"\(.*\)T.*|\1|g')"
102         update-source-version oh-my-zsh "$latestDate" --rev="$latestSha"
103         nixfmt "$default_nix"
104       else
105         echo "${pname} is already up-to-date"
106       fi
107     '';
108   };
110   meta = with lib; {
111     description = "Framework for managing your zsh configuration";
112     longDescription = ''
113       Oh My Zsh is a framework for managing your zsh configuration.
115       To copy the Oh My Zsh configuration file to your home directory, run
116       the following command:
118         $ cp -v $(nix-env -q --out-path oh-my-zsh | cut -d' ' -f3)/share/oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
119     '';
120     homepage = "https://ohmyz.sh/";
121     license = licenses.mit;
122     platforms = platforms.all;
123     maintainers = with maintainers; [ nequissimus ];
124   };