Skip to content

Commit a88aa5a

Browse files
authored
Merge pull request #3938 from rdnetto/force-nix-on-nixos
Enable Nix mode by default when running under NixOS
2 parents b587f5f + b60819c commit a88aa5a

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

src/Stack/Config/Nix.hs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ module Stack.Config.Nix
99
) where
1010

1111
import Stack.Prelude
12+
import Control.Monad.Extra (ifM)
1213
import qualified Data.Text as T
14+
import qualified Data.Text.IO as TIO
1315
import Distribution.System (OS (..))
1416
import Stack.Constants
1517
import Stack.Types.Version
1618
import Stack.Types.Nix
1719
import Stack.Types.Compiler
1820
import Stack.Types.Runner
21+
import System.Directory (doesFileExist)
1922

2023
-- | Interprets NixOptsMonoid options.
2124
nixOptsFromMonoid
@@ -34,12 +37,17 @@ nixOptsFromMonoid NixOptsMonoid{..} os = do
3437
nixShellOptions = fromFirst [] nixMonoidShellOptions
3538
++ prefixAll (T.pack "-I") (fromFirst [] nixMonoidPath)
3639
nixAddGCRoots = fromFirst False nixMonoidAddGCRoots
37-
nixEnable <-
38-
if nixEnable0 && osIsWindows
39-
then do
40-
logInfo "Note: Disabling nix integration, since this is being run in Windows"
41-
return False
42-
else return nixEnable0
40+
41+
osIsNixOS <- isNixOS
42+
nixEnable <- case () of _
43+
| nixEnable0 && osIsWindows -> do
44+
logInfo "Note: Disabling nix integration, since this is being run in Windows"
45+
return False
46+
| not nixEnable0 && osIsNixOS -> do
47+
logInfo "Note: Enabling Nix integration, as it is required under NixOS"
48+
return True
49+
| otherwise -> return nixEnable0
50+
4351
when (not (null nixPackages) && isJust nixInitFile) $
4452
throwIO NixCannotUseShellFileAndPackagesException
4553
return NixOpts{..}
@@ -77,3 +85,10 @@ instance Exception StackNixException
7785
instance Show StackNixException where
7886
show NixCannotUseShellFileAndPackagesException =
7987
"You cannot have packages and a shell-file filled at the same time in your nix-shell configuration."
88+
89+
isNixOS :: MonadIO m => m Bool
90+
isNixOS = liftIO $ do
91+
let fp = "/etc/os-release"
92+
ifM (doesFileExist fp)
93+
(T.isInfixOf "ID=nixos" <$> TIO.readFile fp)
94+
(return False)

0 commit comments

Comments
 (0)