1
0
Fork 0
nixos-config/overlays/writeHaskellScript.nix

121 lines
4.3 KiB
Nix
Raw Normal View History

2021-01-04 18:20:00 +00:00
self: super:
let inherit (self) lib pkgs;
2021-05-18 14:33:28 +00:00
in
{
2020-05-27 13:20:08 +00:00
haskellList = list: ''["${builtins.concatStringsSep ''", "'' list}"]'';
2021-01-04 18:20:00 +00:00
# writeHaskell takes a name, an attrset with libraries and haskell version (both optional)
# and some haskell source code and returns an executable.
#
# Example:
# writeHaskell "missiles" { libraries = [ pkgs.haskellPackages.acme-missiles ]; } ''
# import Acme.Missiles
#
# main = launchMissiles
# '';
writeHaskell = name:
{ libraries ? [ ], ghc ? pkgs.ghc, ghcArgs ? [ ], ghcEnv ? { } }:
2021-05-18 14:33:28 +00:00
pkgs.writers.makeBinWriter
{
compileScript =
let filename = lib.last (builtins.split "/" name);
in
''
cp $contentPath ${filename}.hs
${
lib.concatStringsSep " "
(lib.mapAttrsToList (key: val: ''${key}="${val}"'') ghcEnv)
} ${ghc.withPackages (_: libraries)}/bin/ghc ${
lib.escapeShellArgs ghcArgs
} ${filename}.hs
mv ${filename} $out
${pkgs.binutils-unwrapped}/bin/strip --strip-unneeded "$out"
'';
}
name;
2021-01-04 18:20:00 +00:00
# writeHaskellBin takes the same arguments as writeHaskell but outputs a directory (like writeScriptBin)
writeHaskellBin = name: pkgs.writeHaskell "/bin/${name}";
2020-06-05 22:42:41 +00:00
writeHaskellScript = { name ? "haskell-script", bins ? [ ], imports ? [ ] }:
2020-05-27 13:20:08 +00:00
code:
2021-05-18 14:33:28 +00:00
pkgs.writeHaskellBin name
{
ghcArgs = [
"-threaded"
"-Wall"
"-Wno-unused-top-binds"
"-Wno-missing-signatures"
"-Wno-type-defaults"
"-Wno-unused-imports"
"-Werror"
];
libraries = builtins.attrValues pkgs.myHaskellScriptPackages;
} ''
2020-05-27 13:20:08 +00:00
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ExtendedDefaultRules #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE TupleSections #-}
2020-10-19 13:32:40 +00:00
{-# LANGUAGE PartialTypeSignatures #-}
2020-05-27 13:20:08 +00:00
import Shh
import Relude
import Say
import qualified Relude.Unsafe as Unsafe
import qualified Data.ByteString.Lazy as LBS
import qualified Data.ByteString as BS
import qualified Data.Text as Text
2020-12-16 18:26:20 +00:00
import System.Environment (getArgs, setEnv)
2020-05-27 13:20:08 +00:00
import Control.Exception (bracket, try)
import Data.String.Interpolate (i)
import Control.Concurrent.Async
${builtins.concatStringsSep "\n" (map (x: "import ${x}") imports)}
-- Load binaries from Nix packages. The dependencies will be captured
-- in the closure.
loadFromBins (${
self.haskellList
(builtins.map toString (bins ++ [ self.coreutils self.nix ]))
} :: [String])
getNivPath :: Text -> Text -> IO Text
getNivPath sources channel = do
2020-12-07 02:41:46 +00:00
let expression = [i|(import #{sources}/nix/sources.nix)."#{channel}"|] :: String
2020-05-27 13:20:08 +00:00
nix_build ["-Q", "-E", expression, "--no-out-link"] &> devNull
escaped <- nix_instantiate ["--eval" :: String, "-E", [i|toString #{expression}|]] |> captureTrim
pure . Text.dropAround ('"' ==) . decodeUtf8 . trim $ escaped
2020-12-07 03:12:07 +00:00
aNixPath :: Text -> Text -> Text -> IO [String]
aNixPath homeManagerChannel nixpkgsChannel path = concat <$> mapM getNivAssign
[("home-manager", homeManagerChannel),
("nixpkgs", nixpkgsChannel),
("nixos-unstable", "nixos-unstable")]
2020-05-27 13:20:08 +00:00
where
tag name str = ["-I", [i|#{name :: Text}=#{str :: Text}|]] :: [String]
2020-12-07 02:05:57 +00:00
getNivAssign (name, repo) = tag name <$> getNivPath path repo
2020-05-27 13:20:08 +00:00
2020-12-07 03:12:07 +00:00
myNixPath :: Text -> IO [String]
myNixPath = aNixPath "${self.home-manager-channel}" "${self.nixpkgs-channel}"
2020-10-12 00:33:25 +00:00
buildSystemParams :: [String]
buildSystemParams = ["<nixpkgs/nixos>", "-A", "system"]
2020-10-01 00:32:51 +00:00
remoteBuildParams :: [String]
2021-11-19 18:07:36 +00:00
remoteBuildParams = ["--builders", "@/etc/nix/machines"]
2020-10-01 00:32:51 +00:00
2020-05-27 13:20:08 +00:00
main :: IO ()
${code}
'';
get-niv-path = self.writeHaskellScript { name = "get-niv-path"; } ''
main = do
[sources, channel] <- fmap toText <$> getArgs
path <- getNivPath sources channel
say path
'';
}