Skip to content

Commit

Permalink
Merge pull request #2514 from commercialhaskell/nix-add-gc-roots
Browse files Browse the repository at this point in the history
Added option to add nix dependencies as nix GC roots
  • Loading branch information
YPares authored Aug 23, 2016
2 parents f7f1e4d + d7acb64 commit 8619824
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 3 deletions.
3 changes: 2 additions & 1 deletion ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ Other enhancements:
See [#2243](https://github.com/commercialhaskell/stack/issues/2243)
* Stack/Nix: Sets `LD_LIBRARY_PATH` so packages using C libs for Template Haskell can work
(See _e.g._ [this HaskellR issue](https://github.com/tweag/HaskellR/issues/253))

* Stack/nix: Dependencies can be added as nix GC roots, so they are not removed
when running `nix-collect-garbage`
* Parse CLI arguments and configuration files into less permissive types,
improving error messages for bad inputs.
[#2267](https://github.com/commercialhaskell/stack/issues/2267)
Expand Down
8 changes: 8 additions & 0 deletions doc/nix_integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ nix:
# `[nixpkgs=/my/local/nixpkgs/clone]` that will be used to override
# NIX_PATH.
path: []

# false by default. Whether to add your nix dependencies as nix garbage
# collection roots. This way, calling nix-collect-garbage will not remove
# those packages from the nix store, saving you some time when running
# stack build again with nix support activated.
# This creates a `nix-gc-symlinks` directory in the project `.stack-work`.
# To revert that, just delete this `nix-gc-symlinks` directory.
add-gc-roots: false
```
## Using a custom shell.nix file
Expand Down
1 change: 1 addition & 0 deletions src/Stack/Config/Nix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ nixOptsFromMonoid NixOptsMonoid{..} os = do
nixInitFile = getFirst nixMonoidInitFile
nixShellOptions = fromFirst [] nixMonoidShellOptions
++ prefixAll (T.pack "-I") (fromFirst [] nixMonoidPath)
nixAddGCRoots = fromFirst False nixMonoidAddGCRoots
when (not (null nixPackages) && isJust nixInitFile) $
throwM NixCannotUseShellFileAndPackagesException
return NixOpts{..}
Expand Down
9 changes: 7 additions & 2 deletions src/Stack/Nix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import Stack.Types.Nix
import Stack.Types.Compiler
import Stack.Types.Internal
import System.Environment (lookupEnv,getArgs,getExecutablePath)
import qualified System.FilePath as F
import System.Process.Read (getEnvOverride)

-- | If Nix is enabled, re-runs the currently running OS command in a Nix container.
Expand Down Expand Up @@ -87,6 +88,7 @@ runShellAndExit mprojectRoot getCompilerVersion getCmdArgs = do
pkgs = pkgsInConfig ++ [ghc]
pkgsStr = "[" <> T.intercalate " " pkgs <> "]"
pureShell = nixPureShell (configNix config)
addGCRoots = nixAddGCRoots (configNix config)
nixopts = case mshellFile of
Just fp -> [toFilePath fp, "--arg", "ghc"
,"with (import <nixpkgs> {}); " ++ T.unpack ghc]
Expand All @@ -109,8 +111,11 @@ runShellAndExit mprojectRoot getCompilerVersion getCmdArgs = do
,"STACK_IN_NIX_EXTRA_ARGS = stackExtraArgs; "
,"} \"\""]]
-- glibcLocales is necessary on Linux to avoid warnings about GHC being incapable to set the locale.
fullArgs = concat [if pureShell then ["--pure"] else [],
map T.unpack (nixShellOptions (configNix config))
fullArgs = concat [if pureShell then ["--pure"] else []
,if addGCRoots then ["--indirect", "--add-root"
,toFilePath (configWorkDir config)
F.</> "nix-gc-symlinks" F.</> "gc-root"] else []
,map T.unpack (nixShellOptions (configNix config))
,nixopts
,["--run", intercalate " " (cmnd:"$STACK_IN_NIX_EXTRA_ARGS":args)]
]
Expand Down
3 changes: 3 additions & 0 deletions src/Stack/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,9 @@ nixOptsParser hide0 = overrideActivation <$>
metavar "PATH_OPTIONS" <>
help "Additional options to override NIX_PATH parts (notably 'nixpkgs')" <>
hide))
<*> firstBoolFlags "nix-add-gc-roots"
"addition of packages to the nix GC roots so nix-collect-garbage doesn't remove them"
hide
)
where
hide = hideMods hide0
Expand Down
9 changes: 9 additions & 0 deletions src/Stack/Types/Nix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ data NixOpts = NixOpts
-- ^ The path of a file containing preconfiguration of the environment (e.g shell.nix)
,nixShellOptions :: ![Text]
-- ^ Options to be given to the nix-shell command line
,nixAddGCRoots :: !Bool
-- ^ Should we register gc roots so running nix-collect-garbage doesn't remove nix dependencies
}
deriving (Show)

Expand All @@ -46,6 +48,8 @@ data NixOptsMonoid = NixOptsMonoid
-- ^ Options to be given to the nix-shell command line
,nixMonoidPath :: !(First [Text])
-- ^ Override parts of NIX_PATH (notably 'nixpkgs')
,nixMonoidAddGCRoots :: !(First Bool)
-- ^ Should we register gc roots so running nix-collect-garbage doesn't remove nix dependencies
}
deriving (Eq, Show, Generic)

Expand All @@ -59,6 +63,7 @@ instance FromJSON (WithJSONWarnings NixOptsMonoid) where
nixMonoidInitFile <- First <$> o ..:? nixInitFileArgName
nixMonoidShellOptions <- First <$> o ..:? nixShellOptsArgName
nixMonoidPath <- First <$> o ..:? nixPathArgName
nixMonoidAddGCRoots <- First <$> o ..:? nixAddGCRootsArgName
return NixOptsMonoid{..})

-- | Left-biased combine Nix options
Expand Down Expand Up @@ -89,3 +94,7 @@ nixShellOptsArgName = "nix-shell-options"
-- | NIX_PATH override argument name
nixPathArgName :: Text
nixPathArgName = "path"

-- | Add GC roots arg name
nixAddGCRootsArgName :: Text
nixAddGCRootsArgName = "add-gc-roots"

0 comments on commit 8619824

Please sign in to comment.