Skip to content

Commit

Permalink
Add ignorePackageYaml support to cabalProject (input-output-hk#2094)
Browse files Browse the repository at this point in the history
* add ignorePackageYaml support to cabal project

* move ignorePackageYaml option to project-common
  • Loading branch information
mklca authored Oct 22, 2023
1 parent 1cd599c commit 58dd973
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 10 deletions.
3 changes: 2 additions & 1 deletion lib/call-cabal-project-to-nix.nix
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
# package.
, evalPackages
, supportHpack ? false # Run hpack on package.yaml files with no .cabal file
, ignorePackageYaml ? false # Ignore package.yaml files even if they exist
, ...
}@args:
let
Expand Down Expand Up @@ -494,7 +495,7 @@ let
# run `plan-to-nix` in $out. This should produce files right there with the
# proper relative paths.
(cd $out${subDir'} && plan-to-nix --full --plan-json $tmp${subDir'}/dist-newstyle/cache/plan.json -o .)
(cd $out${subDir'} && plan-to-nix --full ${if ignorePackageYaml then "--ignore-package-yaml" else ""} --plan-json $tmp${subDir'}/dist-newstyle/cache/plan.json -o .)
# Replace the /nix/store paths to minimal git repos with indexes (that will work with materialization).
${fixedProject.replaceLocations}
Expand Down
10 changes: 10 additions & 0 deletions modules/project-common.nix
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,15 @@ with lib.types;
hsPkgs = lib.mkOption {
type = lib.types.unspecified;
};
# Used by stack and cabal projects via
# - ./lib/call-cabal-project-to-nix.nix
# - ./lib/call-stack-to-nix.nix
ignorePackageYaml = mkOption {
type = bool;
default = false;
description = ''
If set, prevents nix-tools from attempting to load package.yaml even if it is present.
'';
};
};
}
4 changes: 0 additions & 4 deletions modules/stack-project.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ with types;
type = str;
default = "stack.yaml";
};
ignorePackageYaml = mkOption {
type = bool;
default = false;
};
cache = mkOption {
type = nullOr unspecified;
default = null;
Expand Down
4 changes: 2 additions & 2 deletions nix-tools/nix-tools/plan2nix/Plan2Nix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ plan2nix args Plan { packages, extras, components, compilerVersion, compilerPack
cwd <- getCurrentDirectory
extrasNix <- fmap (mkNonRecSet . concat) . forM (Map.toList extras) $ \case
(_name, Just (Package v flags (Just (LocalPath folder)) False)) ->
do cabalFiles <- findCabalFiles folder
do cabalFiles <- findCabalFiles (argHpackUse args) folder
forM cabalFiles $ \cabalFile ->
let pkg = cabalFilePkgName cabalFile
nix = ".plan.nix" </> pkg <.> "nix"
Expand Down Expand Up @@ -155,7 +155,7 @@ plan2nix args Plan { packages, extras, components, compilerVersion, compilerPack
cabalFromPath url rev subdir path = do
d <- liftIO $ doesDirectoryExist path
unless d $ fail ("not a directory: " ++ path)
cabalFiles <- liftIO $ findCabalFiles path
cabalFiles <- liftIO $ findCabalFiles (argHpackUse args) path
return $ \sha256 ->
forM cabalFiles $ \cabalFile -> do
let pkg = cabalFilePkgName cabalFile
Expand Down
4 changes: 4 additions & 0 deletions nix-tools/nix-tools/plan2nix/Plan2Nix/CLI.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module Plan2Nix.CLI
( Args(..)
, HpackUse(..)
, parsePlan2NixArgs
) where

import Options.Applicative hiding (option)
import Data.Semigroup ((<>))
import Cabal2Nix (CabalDetailLevel(..))
import Stack2nix.CLI (HpackUse(..))

--------------------------------------------------------------------------------
-- CLI Arguments
Expand All @@ -15,6 +17,7 @@ data Args = Args
, argCabalProject :: FilePath
, argCacheFile :: FilePath
, argDetailLevel :: CabalDetailLevel
, argHpackUse :: HpackUse
} deriving Show

-- Argument Parser
Expand All @@ -25,6 +28,7 @@ args = Args
<*> strOption ( long "cabal-project" <> value "cabal.project" <> showDefault <> metavar "FILE" <> help "Override path to cabal.project" )
<*> strOption ( long "cache" <> value ".nix-tools.cache" <> showDefault <> metavar "FILE" <> help "Dependency cache file" )
<*> flag MinimalDetails FullDetails ( long "full" <> help "Output details needed to determine what files are used" )
<*> flag UsePackageYamlFirst IgnorePackageYaml (long "ignore-package-yaml" <> help "disable hpack run and use only cabal disregarding package.yaml existence")

parsePlan2NixArgs :: IO Args
parsePlan2NixArgs = execParser opts
Expand Down
11 changes: 8 additions & 3 deletions nix-tools/nix-tools/plan2nix/Plan2Nix/Project.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ import qualified Hpack.Config as Hpack
import qualified Hpack.Render as Hpack

import Cabal2Nix (CabalFile(..), CabalFileGenerator(..))
import Plan2Nix.CLI (HpackUse(..))

findCabalFiles :: FilePath -> IO [CabalFile]
findCabalFiles path = doesFileExist (path </> Hpack.packageConfig) >>= \case
False -> fmap (OnDisk . (path </>)) . filter (isSuffixOf ".cabal") <$> listDirectory path
findOnlyCabalFiles :: FilePath -> IO [ CabalFile]
findOnlyCabalFiles path = fmap (OnDisk . (path </>)) . filter (isSuffixOf ".cabal") <$> listDirectory path

findCabalFiles :: HpackUse -> FilePath -> IO [CabalFile]
findCabalFiles IgnorePackageYaml path = findOnlyCabalFiles path
findCabalFiles UsePackageYamlFirst path = doesFileExist (path </> Hpack.packageConfig) >>= \case
False -> findOnlyCabalFiles path
True -> do
mbPkg <- Hpack.readPackageConfig Hpack.defaultDecodeOptions {Hpack.decodeOptionsTarget = path </> Hpack.packageConfig}
case mbPkg of
Expand Down

0 comments on commit 58dd973

Please sign in to comment.