1
1
mirror of https://github.com/nmattia/niv.git synced 2024-09-18 19:07:19 +03:00

Allow custom nixpkgs

This adds a few configuration options to `niv init`:

* `--no-nixpkgs`: skips the import of nixpkgs
* `--nixpkgs-branch`: specifies the branch to use for nixpkgs
* `--nixpkgs`: specifies the repo to use for nixpkgs

NOTE: this changes the default nixpkgs branch from nixos-19.09 to
release-19.09.
This commit is contained in:
Nicolas Mattia 2020-07-07 17:09:01 +02:00
parent fb1716a9c8
commit c27f5a6c57
3 changed files with 64 additions and 13 deletions

View File

@ -351,10 +351,15 @@ Available options:
#### Init
```
Usage: niv init
Usage: niv init ([--no-nixpkgs] | [-b|--nixpkgs-branch ARG]
[--nixpkgs OWNER/REPO])
Initialize a Nix project. Existing files won't be modified.
Available options:
--no-nixpkgs Don't add a nixpkgs entry to sources.json.
-b,--nixpkgs-branch ARG The nixpkgs branch to use. (default: "release-19.09")
--nixpkgs OWNER/REPO Use a custom nixpkgs repository from
GitHub. (default: NixOS/nixpkgs-channels)
-h,--help Show this help text
```

View File

@ -97,17 +97,58 @@ parsePackage = (,) <$> parsePackageName <*> (parsePackageSpec githubCmd)
-- INIT
-------------------------------------------------------------------------------
-- | Whether or not to fetch nixpkgs
data FetchNixpkgs
= NoNixpkgs
| YesNixpkgs T.Text Nixpkgs
data Nixpkgs = Nixpkgs T.Text T.Text -- owner, repo
instance Show Nixpkgs where
show (Nixpkgs o r) = T.unpack o <> "/" <> T.unpack r
-- | The default nixpkgs
defaultNixpkgsRepo, defaultNixpkgsUser :: T.Text
defaultNixpkgsRepo = "nixpkgs-channels"
defaultNixpkgsUser = "NixOS"
parseCmdInit :: Opts.ParserInfo (NIO ())
parseCmdInit = Opts.info (pure cmdInit <**> Opts.helper) $ mconcat desc
parseCmdInit = Opts.info (cmdInit <$> parseNixpkgs <**> Opts.helper) $ mconcat desc
where
customNixpkgsReader = Opts.maybeReader $ \(T.pack -> repo) -> case T.splitOn "/" repo of
[owner, reponame] -> Just (Nixpkgs owner reponame)
_ -> Nothing
parseNixpkgs =
Opts.flag' NoNixpkgs
(
Opts.long "no-nixpkgs" <>
Opts.help "Don't add a nixpkgs entry to sources.json."
) <|>
(YesNixpkgs <$>
(Opts.strOption
(
Opts.long "nixpkgs-branch" <>
Opts.short 'b' <>
Opts.help "The nixpkgs branch to use." <>
Opts.showDefault <>
Opts.value "release-19.09"
)
) <*> Opts.option customNixpkgsReader
(
Opts.long "nixpkgs" <>
Opts.showDefault <>
Opts.help "Use a custom nixpkgs repository from GitHub." <>
Opts.metavar "OWNER/REPO" <>
Opts.value (Nixpkgs "NixOS" "nixpkgs-channels")
))
desc =
[ Opts.fullDesc
, Opts.progDesc
"Initialize a Nix project. Existing files won't be modified."
]
cmdInit :: NIO ()
cmdInit = do
cmdInit :: FetchNixpkgs -> NIO ()
cmdInit nixpkgs = do
job "Initializing" $ do
fsj <- getFindSourcesJson
@ -134,14 +175,19 @@ cmdInit = do
, "repo" .= ("niv" :: T.Text)
]
)
say "Importing 'nixpkgs' ..."
cmdAdd (updateCmd githubCmd) (PackageName "nixpkgs")
(specToFreeAttrs $ PackageSpec $ HMS.fromList
[ "owner" .= ("NixOS" :: T.Text)
, "repo" .= ("nixpkgs-channels" :: T.Text)
, "branch" .= ("nixos-19.09" :: T.Text)
]
)
case nixpkgs of
NoNixpkgs -> say "Not importing 'nixpkgs'."
YesNixpkgs branch nixpkgs' -> do
say "Importing 'nixpkgs' ..."
let (owner, repo) = case nixpkgs' of
Nixpkgs o r -> (o,r)
cmdAdd (updateCmd githubCmd) (PackageName "nixpkgs")
(specToFreeAttrs $ PackageSpec $ HMS.fromList
[ "owner" .= owner
, "repo" .= repo
, "branch" .= branch
]
)
, \path _content -> dontCreateFile path)
] $ \(path, onCreate, onUpdate) -> do
exists <- li $ Dir.doesFileExist path

View File

@ -76,7 +76,7 @@ pkgs.runCommand "test"
cp ${./data/archives + "/${nixpkgs-channels_HEAD}.tar.gz"} \
mock/NixOS/nixpkgs-channels/archive/${nixpkgs-channels_HEAD}.tar.gz
niv init
niv init --nixpkgs NixOS/nixpkgs-channels --nixpkgs-branch nixos-19.09
diff -h ${./expected/niv-init.json} nix/sources.json || \
(echo "Mismatched sources.json"; \
echo "Reference: tests/expected/niv-init.json"; \