1
0
Fork 0

Improve Haskell scripting

This commit is contained in:
Malte Brandy 2020-05-16 15:59:02 +02:00
parent 9a01969199
commit 2b4b6dfe83
No known key found for this signature in database
GPG key ID: 226A2D41EF5378C9
8 changed files with 63 additions and 62 deletions

View file

@ -1,12 +1,13 @@
{ pkgs, lib, ... }: { pkgs, lib, ... }:
let let
inherit (import ../lib) unfreePkgs; inherit (import ../lib) unfreePkgs writeHaskellScript;
inherit (import ../pkgs) my-ssh-add; inherit (import ../pkgs) my-ssh-add;
modes = pkgs.lib.attrNames (import ./modes.nix).apollo; modes = pkgs.lib.attrNames (import ./modes.nix).apollo;
autostart-script = pkgs.writeShellScriptBin "home-manager-autostart" '' autostart-script = pkgs.writeShellScriptBin "home-manager-autostart" ''
${my-ssh-add}/bin/my-ssh-add ${my-ssh-add}/bin/my-ssh-add
${pkgs.xorg.xrdb}/bin/xrdb ${builtins.toFile "Xresources" "Xft.dpi: 96"} ${pkgs.xorg.xrdb}/bin/xrdb ${builtins.toFile "Xresources" "Xft.dpi: 96"}
''; '';
configPath = "/home/maralorn/git/config";
in { in {
xdg.configFile."autostart/home-manager-autostart.desktop".source = "${ xdg.configFile."autostart/home-manager-autostart.desktop".source = "${
@ -16,29 +17,40 @@ in {
exec = "${autostart-script}/bin/home-manager-autostart"; exec = "${autostart-script}/bin/home-manager-autostart";
} }
}/share/applications/home-manager-autostart.desktop"; }/share/applications/home-manager-autostart.desktop";
home.packages = builtins.attrValues { home.packages = builtins.attrValues rec {
maintenance = pkgs.writeShellScriptBin "maintenance" '' maintenance = pkgs.writeShellScriptBin "maintenance" ''
set -e
git -C ~/git/config pull git -C ~/git/config pull
update-modes update-modes
sudo -A update-system sudo -A update-system
sudo -A nix-collect-garbage -d sudo -A nix-collect-garbage -d
sudo -A nix optimise-store sudo -A nix optimise-store
''; '';
activateMode = pkgs.writeShellScriptBin "activate-mode" '' activateMode = writeHaskellScript { name = "activate-mode"; } ''
~/.modes/$(cat ~/tmp/mode)/activate getMode :: IO Text
random-wallpaper getMode = decodeUtf8 <$> (cat "/home/maralorn/tmp/mode" |> captureTrim)
main = do
mode <- getMode
say [i|Switching to mode #{mode}...|]
exe ([i|/home/maralorn/.modes/#{mode}/activate|] :: String)
exe "random-wallpaper"
''; '';
updateModes = pkgs.writeShellScriptBin "update-modes" '' updateModes = writeHaskellScript {
set -e name = "update-modes";
nix build -f ~/git/config/home/target.nix apollo -o ~/.modes bins = [ activateMode ];
activate-mode } ''
main = do
say "Building ~/.modes for apollo"
nixPath <- myNixPath "${configPath}/nix/sources.nix"
nix_build nixPath "${configPath}/home/target.nix" "-A" "apollo" "-o" "/home/maralorn/.modes"
activate_mode
''; '';
selectMode = pkgs.writeShellScriptBin "select-mode" '' selectMode = pkgs.writeShellScriptBin "select-mode" ''
${pkgs.dialog}/bin/dialog --menu "Select Mode" 20 80 5 ${ ${pkgs.dialog}/bin/dialog --menu "Select Mode" 20 80 5 ${
lib.concatStrings (map (mode: "${mode} '' ") modes) lib.concatStrings (map (mode: "${mode} '' ") modes)
} 2> ~/tmp/mode } 2> ~/tmp/mode
clear clear
echo "Switching to mode $(cat ~/tmp/mode)..."
activate-mode > /dev/null activate-mode > /dev/null
''; '';

View file

@ -47,7 +47,7 @@ let
executeFilterMail :: (LByteString, Text, Text) -> IO () executeFilterMail :: (LByteString, Text, Text) -> IO ()
executeFilterMail (files, filter, target) = do executeFilterMail (files, filter, target) = do
putTextLn [i|Sorting "#{filter}" into #{target}|] say [i|Sorting "#{filter}" into #{target}|]
writeOutput files |> mscan writeOutput files |> mscan
mmkdir ([i|${archive}/#{target}|] :: String) mmkdir ([i|${archive}/#{target}|] :: String)
writeOutput files |> mrefile ([i|${archive}/#{target}|] :: String) writeOutput files |> mrefile ([i|${archive}/#{target}|] :: String)

View file

@ -57,6 +57,9 @@ rec {
pkgs.haskellPackages.string-interpolate pkgs.haskellPackages.string-interpolate
pkgs.haskellPackages.relude pkgs.haskellPackages.relude
pkgs.haskellPackages.async pkgs.haskellPackages.async
pkgs.haskellPackages.say
pkgs.haskellPackages.cmdargs
pkgs.haskellPackages.text
]) ])
}/bin/ghc ${name}.hs -threaded }/bin/ghc ${name}.hs -threaded
mv ${name} $out mv ${name} $out
@ -77,6 +80,7 @@ rec {
import Shh import Shh
import Relude import Relude
import Say
import qualified Relude.Unsafe as Unsafe import qualified Relude.Unsafe as Unsafe
import qualified Data.ByteString.Lazy as LBS import qualified Data.ByteString.Lazy as LBS
import qualified Data.ByteString as BS import qualified Data.ByteString as BS
@ -90,25 +94,29 @@ rec {
-- Load binaries from Nix packages. The dependencies will be captured -- Load binaries from Nix packages. The dependencies will be captured
-- in the closure. -- in the closure.
loadFromBins (${ loadFromBins (${
haskellList (builtins.map toString (bins ++ [ pkgs.coreutils ])) haskellList
(builtins.map toString (bins ++ [ pkgs.coreutils pkgs.nix ]))
} :: [String]) } :: [String])
getNivPath :: Text -> Text -> IO Text
getNivPath sources channel = do
let expr = [i|(import #{sources}).#{channel}|] :: String
nix_build ["-Q", "-E", expr, "--no-out-link"] &> devNull
escaped <- nix_instantiate ["--eval" :: String, "-E", [i|toString #{expr}|]] |> captureTrim
pure . Text.dropAround ('"' ==) . decodeUtf8 . trim $ escaped
myNixPath path = concat <$> mapM getNivAssign ["home-manager", "nixpkgs", "unstable"]
where
tag name str = ["-I", [i|#{name :: Text}=#{str :: Text}|]] :: [String]
getNivAssign name = tag name <$> getNivPath path name
${code} ${code}
''; '';
get-niv-path = writeHaskellScript { get-niv-path = writeHaskellScript { name = "get-niv-path"; } ''
name = "get-niv-path";
bins = [ pkgs.nix ];
imports = [ "System.Console.CmdArgs.Implicit" ];
libraries = [ pkgs.haskellPackages.cmdargs pkgs.haskellPackages.text ];
} ''
trimQuotation = pureProc $ encodeUtf8 . Text.dropAround ('"' ==) . decodeUtf8 . trim
main = do main = do
[sources, channel] <- getArgs [sources, channel] <- fmap toText <$> getArgs
let expr = [i|(import #{sources}).#{channel}|] :: String path <- getNivPath sources channel
nix_build ["-Q", "-E", expr, "--no-out-link"] &> devNull say path
nix_instantiate ["--eval" :: String, "-E", [i|toString #{expr}|]] |> trimQuotation
''; '';
home-manager = pkgs.callPackage <home-manager/home-manager> { }; home-manager = pkgs.callPackage <home-manager/home-manager> { };
gcRetentionDays = 5; gcRetentionDays = 5;

View file

@ -1,33 +1,28 @@
let let
inherit (import ../lib) inherit (import ../lib)
pkgs writeHaskellScript get-niv-path home-manager unstable haskellList; pkgs writeHaskellScript home-manager unstable haskellList;
in rec { in rec {
haskellBody = name: commandline: '' haskellBody = name: commandline: ''
getNivPath dir name = get_niv_path ([i|#{dir :: String}/nix/sources.nix|] :: String) name |> captureTrim
getNivAssign dir name = process <$> getNivPath dir name
where process str = ["-I" :: String, [i|#{name :: String}=#{str :: LByteString}|]]
main = do main = do
(configDir:hostname:args) <- getArgs (configDir:hostname:args) <- getArgs
paths <- concat <$> mapM (getNivAssign $ toString configDir) ["nixpkgs", "unstable", "home-manager"] paths <- myNixPath $ toText configDir
putStrLn [i|Trying to build ${name} config for #{hostname}|] say [i|Trying to build ${name} config for #{hostname}|]
${commandline} ${commandline}
''; '';
bins = [ get-niv-path pkgs.nix ]; bins = [ pkgs.nix ];
test-system-config = writeHaskellScript { test-system-config = writeHaskellScript {
name = "test-system-config"; name = "test-system-config";
inherit bins; inherit bins;
} (haskellBody "system" '' } (haskellBody "system" ''
nix $ ["build", "-f", "<nixpkgs/nixos>", "system"] ++ paths ++ ["-I", [i|nixos-config=#{configDir}/hosts/#{hostname}/configuration.nix|], "-o", [i|result-system-#{hostname}|]] ++ fmap toString args nix_build $ ["<nixpkgs/nixos>", "-A", "system"] ++ paths ++ ["-I", [i|nixos-config=#{configDir}/hosts/#{hostname}/configuration.nix|], "-o", [i|result-system-#{hostname}|]] ++ fmap toString args
''); '');
test-home-config = writeHaskellScript { test-home-config = writeHaskellScript {
name = "test-home-config"; name = "test-home-config";
inherit bins; inherit bins;
} (haskellBody "home" '' } (haskellBody "home" ''
nix $ ["build", "-f", "<home-manager/home-manager/home-manager.nix>"] ++ paths ++ ["--argstr", "confPath", [i|#{configDir}/home.nix|], "--argstr", "confAttr", hostname, "--out-link", [i|result-home-manager-#{hostname}|], "activationPackage"] ++ fmap toString args nix_build $ ["<home-manager/home-manager/home-manager.nix>"] ++ paths ++ ["--argstr", "confPath", [i|#{configDir}/home.nix|], "--argstr", "confAttr", hostname, "--out-link", [i|result-home-manager-#{hostname}|], "-A", "activationPackage"] ++ fmap toString args
''); '');
repoSrc = "git@hera.m-0.eu:nixos-config"; repoSrc = "git@hera.m-0.eu:nixos-config";

View file

@ -1,19 +1,13 @@
let inherit (import ./.) home-manager writeHaskellScript get-niv-path; let inherit (import ./.) home-manager writeHaskellScript;
in { in {
update-home = configPath: update-home = configPath:
writeHaskellScript { writeHaskellScript {
name = "update-home"; name = "update-home";
bins = [ get-niv-path home-manager ]; bins = [ home-manager ];
} '' } ''
getNivPath name = get_niv_path "${configPath}/nix/sources.nix" name |> captureTrim
getNivAssign name = tag <$> getNivPath name
where tag str = ["-I", [i|#{name :: String}=#{str :: LByteString}|]] :: [String]
main = do main = do
args <- getArgs args <- getArgs
paths <- concat <$> mapM getNivAssign ["home-manager", "nixpkgs", "unstable"] paths <- myNixPath "${configPath}/nix/sources.nix"
home_manager $ paths ++ ["switch"] ++ fmap toString args home_manager $ paths ++ ["switch"] ++ fmap toString args
''; '';
} }

View file

@ -1,20 +1,13 @@
nixos-rebuild: nixos-rebuild:
let let inherit (import ../lib) writeHaskellScript;
inherit (import ../lib)
writeHaskellScript get-niv-path home-manager gcRetentionDays pkgs;
in rec { in rec {
configPath = "/etc/nixos"; configPath = "/etc/nixos";
update-system = writeHaskellScript { update-system = writeHaskellScript {
name = "update-system"; name = "update-system";
bins = [ get-niv-path nixos-rebuild ]; bins = [ nixos-rebuild ];
} '' } ''
getNivPath name = get_niv_path "${configPath}/nix/sources.nix" name |> captureTrim
getNivAssign name = tag <$> getNivPath name
where tag str = ["-I" :: String, [i|#{name :: String}=#{str :: LByteString}|]]
main = do main = do
paths <- fmap concat . mapM getNivAssign $ ["nixpkgs", "unstable", "home-manager"] paths <- myNixPath "${configPath}/nix/sources.nix"
args <- getArgs args <- getArgs
nixos_rebuild (paths ++ ["switch"] ++ fmap toString args) nixos_rebuild (paths ++ ["switch"] ++ fmap toString args)
''; '';

View file

@ -26,18 +26,18 @@ let
main = do main = do
mirror <- lookupEnv "GL_OPTION_MIRROR" mirror <- lookupEnv "GL_OPTION_MIRROR"
whenJust mirror $ \mirror -> do whenJust mirror $ \mirror -> do
echo ([i|Forwarding push to #{mirror}|] :: String) say [i|Forwarding push to #{mirror}|]
git "push" "--all" "-f" mirror git "push" "--all" "-f" mirror
deploy <- lookupEnv "GL_OPTION_WEB_DEPLOY" deploy <- lookupEnv "GL_OPTION_WEB_DEPLOY"
whenJust deploy $ \deploy -> do whenJust deploy $ \deploy -> do
(maybe [] (\x -> ["-f","default.nix",x]) -> target) <- lookupEnv "GL_OPTION_WEB_DEPLOY_NIX_TARGET" (maybe [] (\x -> ["-f","default.nix",x]) -> target) <- lookupEnv "GL_OPTION_WEB_DEPLOY_NIX_TARGET"
(decodeUtf8 -> path) <- pwd |> captureTrim (decodeUtf8 -> path) <- pwd |> captureTrim
echo ([i|Deploying build to /var/www/#{deploy}|] :: String) say [i|Deploying build to /var/www/#{deploy}|]
bracket (checkout path) (rm "-rf") $ \dir -> withCurrentDirectory dir $ nix "build" "-o" ([i|/var/www/#{deploy}|] :: String) target bracket (checkout path) (rm "-rf") $ \dir -> withCurrentDirectory dir $ nix_build "-o" ([i|/var/www/#{deploy}|] :: String) target
echo "Done" say "Done"
test <- lookupEnv "GL_OPTION_TEST" test <- lookupEnv "GL_OPTION_TEST"
whenJust test $ \_ -> do whenJust test $ \_ -> do
echo "Triggering (an async) system update." say "Triggering (an async) system update."
exe "sudo" ${haskellList update-command}; exe "sudo" ${haskellList update-command};
''; '';
in { in {

View file

@ -30,7 +30,6 @@ in {
anonymousClients.allowedIpRanges = [ "127.0.0.1" "::1" ]; anonymousClients.allowedIpRanges = [ "127.0.0.1" "::1" ];
}; };
}; };
security.pam.services.swaylock = { };
programs.dconf.enable = true; programs.dconf.enable = true;
services = { services = {