Skip to content

Commit 80b555b

Browse files
hackage2nix: use Identifier for bindings in hackage-packages.nix
This increases correctness as toNixName is already used to generate the package expression arguments. If toNixName and attr were to diverge, packages would fail to resolve their dependencies correctly. A side effect of this change is that `pPrint attr` only quotes attribute names as needed, resulting in a huge diff for hackage-packages.nix.
1 parent f8b9cf1 commit 80b555b

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

cabal2nix/cabal2nix.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ executable hackage2nix
107107
, distribution-nixpkgs >= 1.7 && < 1.8
108108
, filepath
109109
, hopenssl >= 2
110-
, language-nix
110+
, language-nix >= 2.3.0
111111
, lens
112112
, monad-par
113113
, monad-par-extras

cabal2nix/hackage2nix/Main.hs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import Distribution.Nixpkgs.Haskell.Constraint
2222
import Distribution.Nixpkgs.Haskell.FromCabal
2323
import Distribution.Nixpkgs.Haskell.FromCabal.Configuration as Config
2424
import Distribution.Nixpkgs.Haskell.FromCabal.Flags
25+
import Distribution.Nixpkgs.Haskell.FromCabal.Name (toNixName)
2526
import Distribution.Nixpkgs.Haskell.Platform ( parsePlatformFromSystemLenient )
2627
import Distribution.Nixpkgs.Haskell.OrphanInstances ( )
2728
import Distribution.Nixpkgs.Meta
@@ -152,8 +153,8 @@ main = do
152153
flagAssignment :: FlagAssignment -- We don't use the flags from Stackage Nightly here, because
153154
flagAssignment = configureCabalFlags pkgId -- they are chosen specifically for GHC 7.10.2.
154155

155-
attr :: String
156-
attr = if isInDefaultPackageSet then unPackageName name else mangle pkgId
156+
attr :: Identifier
157+
attr = if isInDefaultPackageSet then toNixName name else mangle pkgId
157158

158159
drv :: Derivation
159160
drv = fromGenericPackageDescription haskellResolver nixpkgsResolver targetPlatform (compilerInfo config) flagAssignment [] descr
@@ -171,7 +172,7 @@ main = do
171172
overrides :: Doc
172173
overrides = fcat $ punctuate space [ pPrint b <> semi | b <- Set.toList (view (dependencies . each) drv `Set.union` view extraFunctionArgs drv), not (isFromHackage b) ]
173174
return $ render $ nest 2 $
174-
hang (doubleQuotes (text attr) <+> equals <+> text "callPackage") 2 (parens (pPrint drv)) <+> (braces overrides <> semi)
175+
hang (pPrint attr <+> equals <+> text "callPackage") 2 (parens (pPrint drv)) <+> (braces overrides <> semi)
175176

176177
return (intercalate "\n\n" defs)
177178

@@ -215,5 +216,7 @@ resolveConstraint' (PackageVersionConstraint name vrange) hackage
215216
, not (Set.null vset) = Just (Set.findMax vset)
216217
| otherwise = Nothing
217218

218-
mangle :: PackageIdentifier -> String
219-
mangle (PackageIdentifier name v) = unPackageName name ++ '_' : [ if c == '.' then '_' else c | c <- display v ]
219+
mangle :: PackageIdentifier -> Identifier
220+
mangle (PackageIdentifier name v) =
221+
-- Appending a legal Nix identifier to a legal identifier always produces another legal one.
222+
toNixName name & ident %~ (++ '_' : [ if c == '.' then '_' else c | c <- display v ])

0 commit comments

Comments
 (0)