2023-11-17 22:11:18 +03:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2020-05-29 15:40:53 +03:00
|
|
|
|
|
|
|
module NixDaemon where
|
|
|
|
|
2023-11-15 18:01:17 +03:00
|
|
|
import Data.Text (Text)
|
2023-11-22 16:53:03 +03:00
|
|
|
import Data.Either (isRight, isLeft)
|
|
|
|
import Data.Bool (bool)
|
2023-11-29 10:18:53 +03:00
|
|
|
import Control.Monad (forM_, void)
|
2023-11-22 16:53:03 +03:00
|
|
|
import Control.Monad.IO.Class (liftIO)
|
2023-11-15 18:01:17 +03:00
|
|
|
import qualified System.Environment
|
2023-11-22 16:53:03 +03:00
|
|
|
import Control.Exception (bracket)
|
|
|
|
import Control.Concurrent (threadDelay)
|
|
|
|
import qualified Data.ByteString.Char8 as BSC
|
2023-11-12 16:45:33 +03:00
|
|
|
import qualified Data.Either
|
2023-11-22 16:53:03 +03:00
|
|
|
import qualified Data.HashSet as HS
|
|
|
|
import qualified Data.Map.Strict as M
|
2023-11-29 10:18:53 +03:00
|
|
|
import qualified Data.Text
|
|
|
|
import qualified Data.Text.Encoding
|
2023-11-22 16:53:03 +03:00
|
|
|
import System.Directory
|
|
|
|
import System.IO.Temp
|
|
|
|
import qualified System.Process as P
|
|
|
|
import System.Posix.User as U
|
|
|
|
import System.Linux.Namespaces as NS
|
|
|
|
import Test.Hspec (Spec, describe, context)
|
|
|
|
import qualified Test.Hspec as Hspec
|
|
|
|
import Test.Hspec.Expectations.Lifted
|
|
|
|
import System.FilePath
|
|
|
|
import System.Nix.Build
|
|
|
|
import System.Nix.StorePath
|
|
|
|
import System.Nix.StorePath.Metadata
|
|
|
|
import System.Nix.Store.Remote
|
2023-11-30 17:21:16 +03:00
|
|
|
import System.Nix.Store.Remote.MonadStore (mapStoreConfig)
|
2023-11-22 16:53:03 +03:00
|
|
|
|
|
|
|
import Crypto.Hash (SHA256)
|
|
|
|
import System.Nix.Nar (dumpPath)
|
2020-05-29 15:40:53 +03:00
|
|
|
|
2021-02-03 13:44:58 +03:00
|
|
|
createProcessEnv :: FilePath -> String -> [String] -> IO P.ProcessHandle
|
2020-05-29 15:40:53 +03:00
|
|
|
createProcessEnv fp proc args = do
|
2023-11-15 18:01:17 +03:00
|
|
|
mPath <- System.Environment.lookupEnv "PATH"
|
2020-05-29 15:40:53 +03:00
|
|
|
|
2021-02-03 13:44:58 +03:00
|
|
|
(_, _, _, ph) <-
|
|
|
|
P.createProcess (P.proc proc args)
|
2021-08-06 15:38:17 +03:00
|
|
|
{ P.cwd = Just fp
|
2021-02-03 13:44:58 +03:00
|
|
|
, P.env = Just $ mockedEnv mPath fp
|
|
|
|
}
|
2021-02-03 13:52:48 +03:00
|
|
|
pure ph
|
2020-05-29 15:40:53 +03:00
|
|
|
|
|
|
|
mockedEnv :: Maybe String -> FilePath -> [(String, FilePath)]
|
2023-11-15 18:01:17 +03:00
|
|
|
mockedEnv mEnvPath fp =
|
|
|
|
[ ("NIX_STORE_DIR" , fp </> "store")
|
|
|
|
, ("NIX_LOCALSTATE_DIR", fp </> "var")
|
|
|
|
, ("NIX_LOG_DIR" , fp </> "var" </> "log")
|
|
|
|
, ("NIX_STATE_DIR" , fp </> "var" </> "nix")
|
|
|
|
, ("NIX_CONF_DIR" , fp </> "etc")
|
|
|
|
, ("HOME" , fp </> "home")
|
2020-05-29 15:40:53 +03:00
|
|
|
-- , ("NIX_REMOTE", "daemon")
|
2021-08-06 19:18:38 +03:00
|
|
|
] <> foldMap (\x -> [("PATH", x)]) mEnvPath
|
2020-05-29 15:40:53 +03:00
|
|
|
|
|
|
|
waitSocket :: FilePath -> Int -> IO ()
|
2021-01-14 16:15:47 +03:00
|
|
|
waitSocket _ 0 = fail "No socket"
|
2020-05-29 15:40:53 +03:00
|
|
|
waitSocket fp x = do
|
|
|
|
ex <- doesFileExist fp
|
2021-02-03 13:44:58 +03:00
|
|
|
bool
|
|
|
|
(threadDelay 100000 >> waitSocket fp (x - 1))
|
2023-11-15 18:01:17 +03:00
|
|
|
(pure ())
|
2021-02-03 13:44:58 +03:00
|
|
|
ex
|
2020-05-29 15:40:53 +03:00
|
|
|
|
2021-01-14 16:15:47 +03:00
|
|
|
writeConf :: FilePath -> IO ()
|
2021-02-03 13:44:58 +03:00
|
|
|
writeConf fp =
|
2023-11-15 18:01:17 +03:00
|
|
|
writeFile fp $ unlines
|
2021-02-03 13:44:58 +03:00
|
|
|
[ "build-users-group = "
|
2020-05-29 15:40:53 +03:00
|
|
|
, "trusted-users = root"
|
|
|
|
, "allowed-users = *"
|
|
|
|
, "fsync-metadata = false"
|
|
|
|
]
|
|
|
|
|
|
|
|
{-
|
|
|
|
- we run in user namespace as root but groups are failed
|
|
|
|
- => build-users-group has to be empty but we still
|
|
|
|
- get an error (maybe older nix-daemon)
|
|
|
|
-
|
|
|
|
uid=0(root) gid=65534(nobody) groups=65534(nobody)
|
|
|
|
|
|
|
|
drwxr-xr-x 3 0 65534 60 Nov 29 05:53 store
|
|
|
|
|
|
|
|
accepted connection from pid 22959, user root (trusted)
|
|
|
|
error: changing ownership of path '/run/user/1000/test-nix-store-06b0d249e5616122/store': Invalid argument
|
|
|
|
-}
|
|
|
|
|
2021-02-03 13:44:58 +03:00
|
|
|
startDaemon
|
|
|
|
:: FilePath
|
2023-11-30 15:50:57 +03:00
|
|
|
-> IO (P.ProcessHandle, MonadStore a -> IO (Either RemoteStoreError a, [Logger]))
|
2020-05-29 15:40:53 +03:00
|
|
|
startDaemon fp = do
|
|
|
|
writeConf (fp </> "etc" </> "nix.conf")
|
|
|
|
p <- createProcessEnv fp "nix-daemon" []
|
|
|
|
waitSocket sockFp 30
|
2023-06-16 05:28:29 +03:00
|
|
|
pure (p, runStoreOpts sockFp (StoreDir $ BSC.pack $ fp </> "store"))
|
2021-02-03 13:44:58 +03:00
|
|
|
where
|
|
|
|
sockFp = fp </> "var/nix/daemon-socket/socket"
|
2020-05-29 15:40:53 +03:00
|
|
|
|
2021-01-14 16:15:47 +03:00
|
|
|
enterNamespaces :: IO ()
|
2020-05-29 15:40:53 +03:00
|
|
|
enterNamespaces = do
|
|
|
|
uid <- getEffectiveUserID
|
|
|
|
gid <- getEffectiveGroupID
|
|
|
|
|
|
|
|
unshare [User, Network, Mount]
|
2021-02-03 14:18:26 +03:00
|
|
|
-- fmap our (parent) uid to root
|
2020-05-29 15:40:53 +03:00
|
|
|
writeUserMappings Nothing [UserMapping 0 uid 1]
|
2021-02-03 14:18:26 +03:00
|
|
|
-- fmap our (parent) gid to root group
|
2020-05-29 15:40:53 +03:00
|
|
|
writeGroupMappings Nothing [GroupMapping 0 gid 1] True
|
|
|
|
|
2021-01-14 16:15:47 +03:00
|
|
|
withNixDaemon
|
2023-11-30 15:50:57 +03:00
|
|
|
:: ((MonadStore a -> IO (Either RemoteStoreError a, [Logger])) -> IO a) -> IO a
|
2021-02-03 13:44:58 +03:00
|
|
|
withNixDaemon action =
|
2020-05-29 15:40:53 +03:00
|
|
|
withSystemTempDirectory "test-nix-store" $ \path -> do
|
|
|
|
|
|
|
|
mapM_ (createDirectory . snd)
|
2021-02-03 13:44:58 +03:00
|
|
|
(filter ((/= "NIX_REMOTE") . fst) $ mockedEnv Nothing path)
|
2020-05-29 15:40:53 +03:00
|
|
|
|
2021-02-03 13:44:58 +03:00
|
|
|
ini <- createProcessEnv path "nix-store" ["--init"]
|
2021-01-14 16:15:47 +03:00
|
|
|
void $ P.waitForProcess ini
|
2020-05-29 15:40:53 +03:00
|
|
|
|
|
|
|
writeFile (path </> "dummy") "Hello World"
|
|
|
|
|
|
|
|
setCurrentDirectory path
|
|
|
|
|
|
|
|
bracket (startDaemon path)
|
|
|
|
(P.terminateProcess . fst)
|
2021-08-06 15:38:17 +03:00
|
|
|
(action . snd)
|
2020-05-29 15:40:53 +03:00
|
|
|
|
2021-01-14 16:15:47 +03:00
|
|
|
checks :: (Show a, Show b) => IO (a, b) -> (a -> Bool) -> IO ()
|
2020-05-29 15:40:53 +03:00
|
|
|
checks action check = action >>= (`Hspec.shouldSatisfy` (check . fst))
|
2021-01-14 16:15:47 +03:00
|
|
|
|
|
|
|
it
|
|
|
|
:: (Show a, Show b, Monad m)
|
|
|
|
=> String
|
|
|
|
-> m c
|
|
|
|
-> (a -> Bool)
|
|
|
|
-> Hspec.SpecWith (m () -> IO (a, b))
|
2021-02-03 13:44:58 +03:00
|
|
|
it name action check =
|
2023-11-15 18:01:17 +03:00
|
|
|
Hspec.it name $ \run -> run (void $ action) `checks` check
|
2021-01-14 16:15:47 +03:00
|
|
|
|
|
|
|
itRights
|
|
|
|
:: (Show a, Show b, Show c, Monad m)
|
|
|
|
=> String
|
|
|
|
-> m d
|
|
|
|
-> Hspec.SpecWith (m () -> IO (Either a b, c))
|
2020-05-29 15:40:53 +03:00
|
|
|
itRights name action = it name action isRight
|
2021-01-14 16:15:47 +03:00
|
|
|
|
|
|
|
itLefts
|
|
|
|
:: (Show a, Show b, Show c, Monad m)
|
|
|
|
=> String
|
|
|
|
-> m d
|
|
|
|
-> Hspec.SpecWith (m () -> IO (Either a b, c))
|
2020-05-29 15:40:53 +03:00
|
|
|
itLefts name action = it name action isLeft
|
|
|
|
|
2021-01-14 16:15:47 +03:00
|
|
|
withPath :: (StorePath -> MonadStore a) -> MonadStore a
|
2020-05-29 15:40:53 +03:00
|
|
|
withPath action = do
|
2023-11-22 11:04:24 +03:00
|
|
|
path <- addTextToStore "hnix-store" "test" mempty RepairMode_DontRepair
|
2020-05-29 15:40:53 +03:00
|
|
|
action path
|
|
|
|
|
|
|
|
-- | dummy path, adds <tmp>/dummpy with "Hello World" contents
|
2021-01-14 16:15:47 +03:00
|
|
|
dummy :: MonadStore StorePath
|
2020-05-29 15:40:53 +03:00
|
|
|
dummy = do
|
2023-11-12 16:45:33 +03:00
|
|
|
let name = Data.Either.fromRight (error "impossible") $ makeStorePathName "dummy"
|
2023-11-22 11:04:24 +03:00
|
|
|
addToStore @SHA256 name (dumpPath "dummy") FileIngestionMethod_Flat RepairMode_DontRepair
|
2020-05-29 15:40:53 +03:00
|
|
|
|
|
|
|
invalidPath :: StorePath
|
|
|
|
invalidPath =
|
2023-11-12 16:45:33 +03:00
|
|
|
let name = Data.Either.fromRight (error "impossible") $ makeStorePathName "invalid"
|
2023-11-22 11:49:43 +03:00
|
|
|
in unsafeMakeStorePath (mkStorePathHashPart @SHA256 "invalid") name
|
2020-05-29 15:40:53 +03:00
|
|
|
|
2021-01-14 16:15:47 +03:00
|
|
|
withBuilder :: (StorePath -> MonadStore a) -> MonadStore a
|
2020-05-29 15:40:53 +03:00
|
|
|
withBuilder action = do
|
2023-11-22 11:04:24 +03:00
|
|
|
path <- addTextToStore "builder" builderSh mempty RepairMode_DontRepair
|
2020-05-29 15:40:53 +03:00
|
|
|
action path
|
|
|
|
|
2021-01-14 16:15:47 +03:00
|
|
|
builderSh :: Text
|
2021-02-03 13:44:58 +03:00
|
|
|
builderSh = "declare -xpexport > $out"
|
2020-05-29 15:40:53 +03:00
|
|
|
|
|
|
|
spec_protocol :: Spec
|
2021-02-03 13:44:58 +03:00
|
|
|
spec_protocol = Hspec.around withNixDaemon $
|
2020-05-29 15:40:53 +03:00
|
|
|
|
|
|
|
describe "store" $ do
|
|
|
|
|
2021-02-03 13:44:58 +03:00
|
|
|
context "syncWithGC" $
|
2020-05-29 15:40:53 +03:00
|
|
|
itRights "syncs with garbage collector" syncWithGC
|
|
|
|
|
|
|
|
context "verifyStore" $ do
|
2021-02-03 13:44:58 +03:00
|
|
|
itRights "check=False repair=False" $
|
2023-11-23 17:24:39 +03:00
|
|
|
verifyStore
|
|
|
|
CheckMode_DontCheck
|
|
|
|
RepairMode_DontRepair
|
|
|
|
`shouldReturn` False
|
2020-05-29 15:40:53 +03:00
|
|
|
|
2021-02-03 13:44:58 +03:00
|
|
|
itRights "check=True repair=False" $
|
2023-11-23 17:24:39 +03:00
|
|
|
verifyStore
|
|
|
|
CheckMode_DoCheck
|
|
|
|
RepairMode_DontRepair
|
|
|
|
`shouldReturn` False
|
2020-05-29 15:40:53 +03:00
|
|
|
|
|
|
|
--privileged
|
2021-02-03 13:44:58 +03:00
|
|
|
itRights "check=True repair=True" $
|
2023-11-23 17:24:39 +03:00
|
|
|
verifyStore
|
|
|
|
CheckMode_DoCheck
|
|
|
|
RepairMode_DoRepair
|
|
|
|
`shouldReturn` False
|
2020-05-29 15:40:53 +03:00
|
|
|
|
2021-02-03 13:44:58 +03:00
|
|
|
context "addTextToStore" $
|
2021-08-06 19:18:38 +03:00
|
|
|
itRights "adds text to store" $ withPath pure
|
2020-05-29 15:40:53 +03:00
|
|
|
|
|
|
|
context "isValidPathUncached" $ do
|
|
|
|
itRights "validates path" $ withPath $ \path -> do
|
2021-08-06 15:38:17 +03:00
|
|
|
liftIO $ print path
|
|
|
|
isValidPathUncached path `shouldReturn` True
|
2023-11-30 15:50:57 +03:00
|
|
|
itLefts "fails on invalid path"
|
|
|
|
$ mapStoreConfig
|
|
|
|
(\sc -> sc { storeConfig_dir = StoreDir "/asdf" })
|
|
|
|
$ isValidPathUncached invalidPath
|
2020-05-29 15:40:53 +03:00
|
|
|
|
|
|
|
context "queryAllValidPaths" $ do
|
2021-08-06 15:38:17 +03:00
|
|
|
itRights "empty query" queryAllValidPaths
|
2021-02-03 13:44:58 +03:00
|
|
|
itRights "non-empty query" $ withPath $ \path ->
|
2021-08-06 15:38:17 +03:00
|
|
|
queryAllValidPaths `shouldReturn` HS.fromList [path]
|
2020-05-29 15:40:53 +03:00
|
|
|
|
2021-02-03 13:44:58 +03:00
|
|
|
context "queryPathInfoUncached" $
|
2023-11-22 00:41:47 +03:00
|
|
|
itRights "queries path info" $ withPath $ \path -> do
|
|
|
|
meta <- queryPathInfoUncached path
|
|
|
|
references meta `shouldSatisfy` HS.null
|
2020-05-29 15:40:53 +03:00
|
|
|
|
2021-02-03 13:44:58 +03:00
|
|
|
context "ensurePath" $
|
2021-08-06 15:38:17 +03:00
|
|
|
itRights "simple ensure" $ withPath ensurePath
|
2020-05-29 15:40:53 +03:00
|
|
|
|
2021-02-03 13:44:58 +03:00
|
|
|
context "addTempRoot" $
|
2021-08-06 15:38:17 +03:00
|
|
|
itRights "simple addition" $ withPath addTempRoot
|
2020-05-29 15:40:53 +03:00
|
|
|
|
2021-02-03 13:44:58 +03:00
|
|
|
context "addIndirectRoot" $
|
2021-08-06 15:38:17 +03:00
|
|
|
itRights "simple addition" $ withPath addIndirectRoot
|
2020-05-29 15:40:53 +03:00
|
|
|
|
|
|
|
context "buildPaths" $ do
|
|
|
|
itRights "build Normal" $ withPath $ \path -> do
|
|
|
|
let pathSet = HS.fromList [path]
|
2023-11-23 19:07:19 +03:00
|
|
|
buildPaths pathSet BuildMode_Normal
|
2020-05-29 15:40:53 +03:00
|
|
|
|
|
|
|
itRights "build Check" $ withPath $ \path -> do
|
|
|
|
let pathSet = HS.fromList [path]
|
2023-11-23 19:07:19 +03:00
|
|
|
buildPaths pathSet BuildMode_Check
|
2020-05-29 15:40:53 +03:00
|
|
|
|
|
|
|
itLefts "build Repair" $ withPath $ \path -> do
|
|
|
|
let pathSet = HS.fromList [path]
|
2023-11-23 19:07:19 +03:00
|
|
|
buildPaths pathSet BuildMode_Repair
|
2020-05-29 15:40:53 +03:00
|
|
|
|
2021-02-03 13:44:58 +03:00
|
|
|
context "roots" $ context "findRoots" $ do
|
2021-08-06 15:38:17 +03:00
|
|
|
itRights "empty roots" (findRoots `shouldReturn` M.empty)
|
2020-05-29 15:40:53 +03:00
|
|
|
|
2021-01-14 16:15:47 +03:00
|
|
|
itRights "path added as a temp root" $ withPath $ \_ -> do
|
2020-05-29 15:40:53 +03:00
|
|
|
roots <- findRoots
|
2021-02-03 13:44:58 +03:00
|
|
|
roots `shouldSatisfy` ((== 1) . M.size)
|
2020-05-29 15:40:53 +03:00
|
|
|
|
2021-08-06 15:38:17 +03:00
|
|
|
context "optimiseStore" $ itRights "optimises" optimiseStore
|
2020-05-29 15:40:53 +03:00
|
|
|
|
2021-02-03 13:44:58 +03:00
|
|
|
context "queryMissing" $
|
2020-05-29 15:40:53 +03:00
|
|
|
itRights "queries" $ withPath $ \path -> do
|
|
|
|
let pathSet = HS.fromList [path]
|
2023-12-02 16:45:05 +03:00
|
|
|
queryMissing pathSet
|
|
|
|
`shouldReturn`
|
|
|
|
Missing
|
|
|
|
{ missingWillBuild = mempty
|
|
|
|
, missingWillSubstitute = mempty
|
|
|
|
, missingUnknownPaths = mempty
|
|
|
|
, missingDownloadSize = 0
|
|
|
|
, missingNarSize = 0
|
|
|
|
}
|
2020-05-29 15:40:53 +03:00
|
|
|
|
2021-02-03 13:44:58 +03:00
|
|
|
context "addToStore" $
|
2020-05-29 15:40:53 +03:00
|
|
|
itRights "adds file to store" $ do
|
|
|
|
fp <- liftIO $ writeSystemTempFile "addition" "lal"
|
2023-11-12 16:45:33 +03:00
|
|
|
let name = Data.Either.fromRight (error "impossible") $ makeStorePathName "tmp-addition"
|
2023-11-22 11:04:24 +03:00
|
|
|
res <- addToStore @SHA256 name (dumpPath fp) FileIngestionMethod_Flat RepairMode_DontRepair
|
2020-05-29 15:40:53 +03:00
|
|
|
liftIO $ print res
|
|
|
|
|
|
|
|
context "with dummy" $ do
|
|
|
|
itRights "adds dummy" dummy
|
|
|
|
|
|
|
|
itRights "valid dummy" $ do
|
|
|
|
path <- dummy
|
2021-08-06 15:38:17 +03:00
|
|
|
liftIO $ print path
|
|
|
|
isValidPathUncached path `shouldReturn` True
|
2023-11-29 10:18:53 +03:00
|
|
|
|
|
|
|
context "deleteSpecific" $
|
|
|
|
itRights "delete a path from the store" $ withPath $ \path -> do
|
|
|
|
-- clear temp gc roots so the delete works. restarting the nix daemon should also do this...
|
|
|
|
storeDir <- getStoreDir
|
|
|
|
let tempRootsDir = Data.Text.unpack $ mconcat [ Data.Text.Encoding.decodeUtf8 (unStoreDir storeDir), "/../var/nix/temproots/" ]
|
|
|
|
tempRootList <- liftIO $ listDirectory tempRootsDir
|
|
|
|
liftIO $ forM_ tempRootList $ \entry -> do
|
|
|
|
removeFile $ mconcat [ tempRootsDir, "/", entry ]
|
|
|
|
|
2023-11-30 08:53:40 +03:00
|
|
|
GCResult{..} <- deleteSpecific (HS.fromList [path])
|
|
|
|
gcResult_deletedPaths `shouldBe` HS.fromList [path]
|
|
|
|
gcResult_bytesFreed `shouldBe` 4
|
2023-11-29 10:18:53 +03:00
|
|
|
|