summaryrefslogtreecommitdiff
path: root/custom/notify-user.nix
blob: d3e69bcdc81245d83f44b2b7ab7b644fd402a2ff (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
{ stdenv, writeTextFile
, ghcWithPackages
, user ? "gkleen"
, libnotify
}:

stdenv.mkDerivation {
  name = ''notify-${user}'';
  src = writeTextFile { name = ''notify-${user}.hs''; text = ''
    import System.FilePath.Glob (glob)
    import System.Environment (setEnv, getArgs)
    import System.Process (callProcess)
    import System.Posix.Escape (escape)

    import Data.List (isPrefixOf, dropWhileEnd)
    import Data.Char (isSpace)

    import Control.Monad (forM_)
    
    main = do
      envFiles <- glob "/home/${user}/.dbus/session-bus/*"
      forM_ envFiles $ \envFile -> do
        sessionAddr <- tail . snd . break (== '=') . head . filter ("DBUS_SESSION_BUS_ADDRESS=" `isPrefixOf`) . lines <$> readFile envFile
        setEnv "DBUS_SESSION_BUS_ADDRESS" sessionAddr
        callProcess "${libnotify}/bin/notify-send" =<< (++) <$> getArgs <*> (pure . escape . dropWhileEnd isSpace <$> getContents)
      
  ''; };
  phases = [ "buildPhase" "installPhase" ];
  buildPhase = ''
    ${ghcWithPackages (p: with p; [ Glob process posix-escape ])}/bin/ghc -odir . -hidir . $src -o notify-${user}
  '';
  installPhase = ''
    mkdir -p $out/bin
    cp notify-${user} $out/bin
    chmod +x $out/bin/notify-${user}
  '';
}