1{
2 lib,
3 stdenv,
4 stdenvNoCC,
5 fetchFromGitHub,
6 fetchurl,
7 swift,
8 swiftpm,
9 swiftpm2nix,
10 swiftPackages,
11 libarchive,
12 p7zip,
13 # Building from source on x86_64 fails (among other things) due to:
14 # error: cannot load underlying module for 'Darwin'
15 fromSource ? (stdenv.system != "x86_64-darwin"),
16}:
17
18let
19 generated = swiftpm2nix.helpers ./generated;
20
21 pname = "dockutil";
22 version = "3.1.3";
23
24 meta = with lib; {
25 description = "Tool for managing dock items";
26 homepage = "https://github.com/kcrawford/dockutil";
27 license = licenses.asl20;
28 maintainers = with maintainers; [ tboerger ];
29 mainProgram = "dockutil";
30 platforms = platforms.darwin;
31 };
32
33 buildFromSource = swiftPackages.stdenv.mkDerivation (finalAttrs: {
34 inherit pname version meta;
35
36 src = fetchFromGitHub {
37 owner = "kcrawford";
38 repo = "dockutil";
39 rev = finalAttrs.version;
40 hash = "sha256-mmk4vVZhq4kt05nI/dDM1676FDWyf4wTSwY2YzqKsLU=";
41 };
42
43 postPatch = ''
44 # Patch sources so that they build with Swift CoreFoundation
45 # which differs ever so slightly from Apple's implementation.
46 substituteInPlace Sources/DockUtil/DockUtil.swift \
47 --replace-fail "URL(filePath:" \
48 "URL(fileURLWithPath:" \
49 --replace-fail "path(percentEncoded: false)" \
50 "path"
51 '';
52
53 nativeBuildInputs = [
54 swift
55 swiftpm
56 ];
57
58 configurePhase = generated.configure;
59
60 installPhase = ''
61 runHook preInstall
62 install -Dm755 .build/${stdenv.hostPlatform.darwinArch}-apple-macosx/release/dockutil -t $out/bin
63 runHook postInstall
64 '';
65 });
66
67 installBinary = stdenvNoCC.mkDerivation (finalAttrs: {
68 inherit pname version;
69
70 src = fetchurl {
71 url = "https://github.com/kcrawford/dockutil/releases/download/${finalAttrs.version}/dockutil-${finalAttrs.version}.pkg";
72 hash = "sha256-9g24Jz/oDXxIJFiL7bU4pTh2dcORftsAENq59S0/JYI=";
73 };
74
75 dontPatch = true;
76 dontConfigure = true;
77 dontBuild = true;
78
79 nativeBuildInputs = [
80 libarchive
81 p7zip
82 ];
83
84 unpackPhase = ''
85 7z x $src
86 bsdtar -xf Payload~
87 '';
88
89 installPhase = ''
90 runHook preInstall
91 mkdir -p $out/bin
92 install -Dm755 usr/local/bin/dockutil -t $out/bin
93 runHook postInstall
94 '';
95
96 meta = meta // {
97 sourceProvenance = [ lib.sourceTypes.binaryNativeCode ];
98 };
99 });
100in
101if fromSource then buildFromSource else installBinary