|
| 1 | +{-# LANGUAGE OverloadedStrings #-} |
| 2 | +{-# LANGUAGE DataKinds #-} |
| 3 | +{-# LANGUAGE TypeApplications #-} |
| 4 | + |
| 5 | +module Derivation where |
| 6 | + |
| 7 | +import Control.Monad.IO.Class (liftIO) |
| 8 | + |
| 9 | +import Data.Text (Text) |
| 10 | +import Nix.Derivation (Derivation(..), DerivationOutput(..)) |
| 11 | +import System.Nix.StorePath (StorePath, storePathToText) |
| 12 | + |
| 13 | +import System.Nix.Store.Remote (addToStore, addTextToStore) |
| 14 | +import System.Nix.Hash (HashAlgorithm(Truncated, SHA256)) |
| 15 | + |
| 16 | +import qualified Data.Map |
| 17 | +import qualified Data.Set |
| 18 | +import qualified Data.Text |
| 19 | +import qualified Data.Text.Lazy |
| 20 | +import qualified Data.Text.Lazy.Builder |
| 21 | +import qualified Data.Vector |
| 22 | +import qualified Nix.Derivation |
| 23 | +import qualified System.Process |
| 24 | +import qualified System.Nix.Derivation |
| 25 | +import qualified System.Nix.StorePath |
| 26 | + |
| 27 | +drvSample :: StorePath -> StorePath -> StorePath -> Derivation StorePath Text |
| 28 | +drvSample builder buildScript out = Derivation { |
| 29 | + outputs = Data.Map.fromList [ ("out", DerivationOutput out "sha256" "test") ] |
| 30 | + , inputDrvs = Data.Map.fromList [ (builder, Data.Set.fromList [ "out" ]) ] |
| 31 | + , inputSrcs = Data.Set.fromList [ buildScript ] |
| 32 | + , platform = "x86_64-linux" |
| 33 | + , builder = storePathToText builder |
| 34 | + , args = Data.Vector.fromList ["-e", storePathToText buildScript ] |
| 35 | + , env = Data.Map.fromList [("testEnv", "true")] |
| 36 | + } |
| 37 | + |
| 38 | +withBash action = do |
| 39 | + fp <- fmap init <$> liftIO $ System.Process.readProcess "which" ["bash"] "" |
| 40 | + let Right n = System.Nix.StorePath.makeStorePathName "bash" |
| 41 | + path <- addToStore @SHA256 n fp False (pure True) False |
| 42 | + action path |
| 43 | + |
| 44 | +withBuildScript action = do |
| 45 | + path <- addTextToStore |
| 46 | + "buildScript" |
| 47 | + (Data.Text.concat [ "declare -xp", "export > $out" ]) |
| 48 | + mempty |
| 49 | + False |
| 50 | + |
| 51 | + action path |
| 52 | + |
| 53 | +withDerivation action = withBuildScript $ \buildScript -> withBash $ \bash -> do |
| 54 | + outputPath <- addTextToStore "wannabe-output" "" mempty False |
| 55 | + |
| 56 | + let d = drvSample bash buildScript outputPath |
| 57 | + |
| 58 | + path <- addTextToStore |
| 59 | + "hnix-store-derivation" |
| 60 | + (Data.Text.Lazy.toStrict |
| 61 | + $ Data.Text.Lazy.Builder.toLazyText |
| 62 | + $ System.Nix.Derivation.buildDerivation d |
| 63 | + ) |
| 64 | + mempty |
| 65 | + False |
| 66 | + --(HS.fromList []) False |
| 67 | + liftIO $ print d |
| 68 | + action path d |
| 69 | + |
0 commit comments