summaryrefslogtreecommitdiff
path: root/custom/notify-user.nix
blob: e9e98ddc40384336d08275790906fc5f79e154c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
{ stdenv, writeTextFile
, ghcWithPackages
, user ? "gkleen"
# , libnotify
}:

stdenv.mkDerivation {
  name = ''notify-${user}'';
  src = writeTextFile { name = ''notify-${user}.hs''; text = ''
    {-# LANGUAGE ViewPatterns, StandaloneDeriving #-}
  
    import System.FilePath.Glob (glob)
    import System.Environment (setEnv, getArgs)
    import System.Process (spawnProcess, waitForProcess)
    import System.Exit (exitWith, ExitCode(..))

    import Data.List (isPrefixOf, dropWhile, dropWhileEnd, init)
    import Data.Char (isSpace, toLower, toUpper)

    import Control.Monad (forM_, void)

    import qualified Libnotify as Notify
    import Data.Monoid

    import System.Console.GetOpt.Simple

    import qualified Data.Map as Map

    import Data.Maybe
    import Text.Read (readMaybe)

    deriving instance Read Notify.Urgency

    main = do
      envFiles <- glob "/home/${user}/.dbus/session-bus/*"
      forM_ envFiles $ \envFile -> do
        sessionAddr <- unQuote . tail . snd . break (== '=') . head . filter ("DBUS_SESSION_BUS_ADDRESS=" `isPrefixOf`) . lines <$> readFile envFile
        setEnv "DBUS_SESSION_BUS_ADDRESS" sessionAddr
        lines <- lines <$> getContents
        case lines of
          ((trim -> summary):(trim . unlines -> contents)) -> do
            (opts, _) <- flip getUsingConf [] [ (arg, "urgency", Optional, "")
                                              , (arg, "app-name", Optional, "")
                                              , (arg, "category", Optional, "")
                                              ]
            let
              urgency = fromMaybe Notify.Normal $ readMaybe . caseForRead =<< Map.lookup "urgency" opts
              appName = fromMaybe "notify-${user}" $ Map.lookup "app-name" opts
              category = fromMaybe "" $ Map.lookup "category" opts
            Notify.display_ $ Notify.summary summary <> Notify.body contents <> Notify.appName appName <> Notify.urgency urgency <> Notify.category category
          _ -> exitWith $ ExitFailure 2
      where
        trim = dropWhileEnd isSpace . dropWhile isSpace
        unQuote ('\''':xs) = init xs
        unQuote ('"':xs) = init xs
        unQuote xs = xs
        caseForRead [] = []
        caseForRead (x:xs) = toUpper x : map toLower xs
  ''; };
  phases = [ "buildPhase" "installPhase" ];
  buildPhase = ''
    ${ghcWithPackages (p: with p; [ Glob process libnotify getopt-simple containers ])}/bin/ghc -odir . -hidir . $src -o notify-${user}
  '';
  installPhase = ''
    mkdir -p $out/bin
    cp notify-${user} $out/bin
    chmod +x $out/bin/notify-${user}
  '';
}