btrbk: add mainProgram (#356350)
[NixPkgs.git] / pkgs / by-name / ke / keepass / package.nix
blobda3c446a801e27bf6a9ec5acd8a62bb5729b70a2
1 { lib, stdenv, fetchurl
2 , unzip, mono, makeWrapper, icoutils
3 , substituteAll, xsel, xorg, xdotool, coreutils, unixtools, glib
4 , gtk2, makeDesktopItem, plugins ? [] }:
6 stdenv.mkDerivation (finalAttrs: {
7   pname = "keepass";
8   version = "2.57.1";
10   src = fetchurl {
11     url = "mirror://sourceforge/keepass/KeePass-${finalAttrs.version}-Source.zip";
12     hash = "sha256-97ZX1EzhMv4B3YZ3HoUqlGTEMsQn3cmNGr+uvS6AKYY=";
13   };
15   sourceRoot = ".";
17   nativeBuildInputs = [
18     unzip
19     mono
20     makeWrapper
21   ];
22   buildInputs = [ icoutils ];
24   patches = [
25     (substituteAll {
26       src = ./fix-paths.patch;
27       xsel = "${xsel}/bin/xsel";
28       xprop = "${xorg.xprop}/bin/xprop";
29       xdotool = "${xdotool}/bin/xdotool";
30       uname = "${coreutils}/bin/uname";
31       whereis = "${unixtools.whereis}/bin/whereis";
32       gsettings = "${glib}/bin/gsettings";
33     })
34   ];
36   # KeePass looks for plugins in under directory in which KeePass.exe is
37   # located. It follows symlinks where looking for that directory, so
38   # buildEnv is not enough to bring KeePass and plugins together.
39   #
40   # This derivation patches KeePass to search for plugins in specified
41   # plugin derivations in the Nix store and nowhere else.
42   pluginLoadPathsPatch = let
43     inherit (builtins) toString;
44     inherit (lib.strings) readFile concatStrings replaceStrings unsafeDiscardStringContext;
45     inherit (lib.lists) map length;
46     inherit (lib) add;
48     outputLc = toString (add 7 (length plugins));
49     patchTemplate = readFile ./keepass-plugins.patch;
50     loadTemplate  = readFile ./keepass-plugins-load.patch;
51     loads = concatStrings
52       (map
53         (p: replaceStrings ["$PATH$"] [ (unsafeDiscardStringContext (toString p)) ] loadTemplate)
54           plugins);
55   in
56   replaceStrings ["$OUTPUT_LC$" "$DO_LOADS$"] [outputLc loads] patchTemplate;
58   passAsFile = [ "pluginLoadPathsPatch" ];
59   postPatch = ''
60     sed -i 's/\r*$//' KeePass/Forms/MainForm.cs
61     patch -p1 <$pluginLoadPathsPatchPath
62   '';
64   configurePhase = ''
65     runHook preConfigure
67     rm -rvf Build/*
68     find . -name "*.sln" -print -exec sed -i 's/Format Version 10.00/Format Version 11.00/g' {} \;
69     find . -name "*.csproj" -print -exec sed -i '
70       s#ToolsVersion="3.5"#ToolsVersion="4.0"#g
71       s#<TargetFrameworkVersion>.*</TargetFrameworkVersion>##g
72       s#<PropertyGroup>#<PropertyGroup><TargetFrameworkVersion>v4.5</TargetFrameworkVersion>#g
73       s#<SignAssembly>.*$#<SignAssembly>false</SignAssembly>#g
74       s#<PostBuildEvent>.*sgen.exe.*$##
75     ' {} \;
77     runHook postConfigure
78   '';
80   buildPhase = ''
81     runHook preBuild
83     xbuild /p:Configuration=Release
85     runHook postBuild
86   '';
88   outputFiles = [
89     "Build/KeePass/Release/*"
90     "Build/KeePassLib/Release/*"
91     "Ext/KeePass.config.xml" # contains <PreferUserConfiguration>true</PreferUserConfiguration>
92   ];
94   # plgx plugin like keefox requires mono to compile at runtime
95   # after loading. It is brought into plugins bin/ directory using
96   # buildEnv in the plugin derivation. Wrapper below makes sure it
97   # is found and does not pollute output path.
98   binPaths = lib.concatStringsSep ":" (map (x: x + "/bin") plugins);
100   dynlibPath = lib.makeLibraryPath [ gtk2 ];
102   installPhase = ''
103     runHook preInstall
105     target="$out/lib/dotnet/${finalAttrs.pname}"
106     mkdir -p "$target"
108     cp -rv $outputFiles "$target"
110     makeWrapper \
111       "${mono}/bin/mono" \
112       "$out/bin/keepass" \
113       --add-flags "$target/KeePass.exe" \
114       --prefix PATH : "$binPaths" \
115       --prefix LD_LIBRARY_PATH : "$dynlibPath"
117     # setup desktop item with icon
118     mkdir -p "$out/share/applications"
119     cp $desktopItem/share/applications/* $out/share/applications
121     ${./extractWinRscIconsToStdFreeDesktopDir.sh} \
122       "./Translation/TrlUtil/Resources/KeePass.ico" \
123       '[^\.]+_[0-9]+_([0-9]+x[0-9]+)x[0-9]+\.png' \
124       '\1' \
125       '([^\.]+).+' \
126       'keepass' \
127       "$out" \
128       "./tmp"
129     runHook postInstall
130   '';
132   desktopItem = makeDesktopItem {
133     name = "keepass";
134     exec = "keepass";
135     comment = "Password manager";
136     icon = "keepass";
137     desktopName = "Keepass";
138     genericName = "Password manager";
139     categories = [ "Utility" ];
140     mimeTypes = [ "application/x-keepass2" ];
141   };
143   meta = {
144     description = "GUI password manager with strong cryptography";
145     homepage = "http://www.keepass.info/";
146     maintainers = with lib.maintainers; [ amorsillo obadz ];
147     platforms = with lib.platforms; all;
148     license = lib.licenses.gpl2;
149     mainProgram = "keepass";
150   };