1
1
mirror of https://github.com/nmattia/niv.git synced 2024-11-25 09:56:49 +03:00

Speed up initialization

This speeds up the `niv init` command by getting rid of
`nix-prefetch-url` calls (by default). In particular:
* `niv` itself is not added anymore
* `nixpkgs` is fetched from a "known" version
This commit is contained in:
Nicolas Mattia 2021-06-07 12:27:12 +02:00
parent 1819632b58
commit 527494090f
6 changed files with 104 additions and 69 deletions

View File

@ -364,15 +364,19 @@ Available options:
#### Init #### Init
``` ```
Usage: niv init [--no-nixpkgs | [-b|--nixpkgs-branch ARG] Usage: niv init [--fast | --latest | --nixpkgs OWNER/REPO
[--nixpkgs OWNER/REPO]] (-b|--nixpkgs-branch ARG) |
--no-nixpkgs]
Initialize a Nix project. Existing files won't be modified. Initialize a Nix project. Existing files won't be modified.
Available options: Available options:
--no-nixpkgs Don't add a nixpkgs entry to sources.json. --fast Use the latest nixpkgs cached at
-b,--nixpkgs-branch ARG The nixpkgs branch to use. (default: "release-20.03") 'https://github.com/nmattia/niv/blob/master/data/nixpkgs.json'.
This is the default.
--latest Pull the latest unstable nixpkgs from NixOS/nixpkgs.
--nixpkgs OWNER/REPO Use a custom nixpkgs repository from GitHub. --nixpkgs OWNER/REPO Use a custom nixpkgs repository from GitHub.
(default: NixOS/nixpkgs) -b,--nixpkgs-branch ARG The nixpkgs branch when using --nixpkgs ....
--no-nixpkgs Don't add a nixpkgs entry to sources.json.
-h,--help Show this help text -h,--help Show this help text
``` ```

4
data/README.md Normal file
View File

@ -0,0 +1,4 @@
# Data
`./nixpkgs.json` is the default value used by niv when initializing `nixpkgs`.
The executable fetches the latest version from the repo.

12
data/nixpkgs.json Normal file
View File

@ -0,0 +1,12 @@
{
"branch": "release-21.05",
"description": "Nix Packages collection",
"homepage": "",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "5f244caea76105b63d826911b2a1563d33ff1cdc",
"sha256": "1xlgynfw9svy7nvh9nkxsxdzncv9hg99gbvbwv3gmrhmzc3sar75",
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs/archive/5f244caea76105b63d826911b2a1563d33ff1cdc.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
}

View File

@ -22,6 +22,7 @@ import Data.Hashable (Hashable)
import qualified Data.Text as T import qualified Data.Text as T
import Data.Text.Extended import Data.Text.Extended
import Data.Version (showVersion) import Data.Version (showVersion)
import qualified Network.HTTP.Simple as HTTP
import Niv.Cmd import Niv.Cmd
import Niv.Git.Cmd import Niv.Git.Cmd
import Niv.GitHub.Cmd import Niv.GitHub.Cmd
@ -118,55 +119,66 @@ parsePackage = (,) <$> parsePackageName <*> (parsePackageSpec githubCmd)
-- | Whether or not to fetch nixpkgs -- | Whether or not to fetch nixpkgs
data FetchNixpkgs data FetchNixpkgs
= NoNixpkgs = NoNixpkgs
| YesNixpkgs T.Text Nixpkgs -- branch, nixpkgs | NixpkgsFast -- Pull latest known nixpkgs
| NixpkgsCustom T.Text Nixpkgs -- branch, nixpkgs
deriving (Show)
data Nixpkgs = Nixpkgs T.Text T.Text -- owner, repo data Nixpkgs = Nixpkgs T.Text T.Text -- owner, repo
instance Show Nixpkgs where instance Show Nixpkgs where
show (Nixpkgs o r) = T.unpack o <> "/" <> T.unpack r show (Nixpkgs o r) = T.unpack o <> "/" <> T.unpack r
-- | The default nixpkgs
defaultNixpkgsRepo, defaultNixpkgsUser, defaultNixpkgsBranch :: T.Text
defaultNixpkgsRepo = "nixpkgs"
defaultNixpkgsUser = "NixOS"
defaultNixpkgsBranch = "release-20.03"
parseCmdInit :: Opts.ParserInfo (NIO ()) parseCmdInit :: Opts.ParserInfo (NIO ())
parseCmdInit = Opts.info (cmdInit <$> parseNixpkgs <**> Opts.helper) $ mconcat desc parseCmdInit = Opts.info (cmdInit <$> parseNixpkgs <**> Opts.helper) $ mconcat desc
where 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 defaultNixpkgsBranch
)
)
<*> Opts.option
customNixpkgsReader
( Opts.long "nixpkgs"
<> Opts.showDefault
<> Opts.help "Use a custom nixpkgs repository from GitHub."
<> Opts.metavar "OWNER/REPO"
<> Opts.value (Nixpkgs defaultNixpkgsUser defaultNixpkgsRepo)
)
)
desc = desc =
[ Opts.fullDesc, [ Opts.fullDesc,
Opts.progDesc Opts.progDesc
"Initialize a Nix project. Existing files won't be modified." "Initialize a Nix project. Existing files won't be modified."
] ]
parseNixpkgs :: Opts.Parser FetchNixpkgs
parseNixpkgs = parseNixpkgsFast <|> parseNixpkgsLatest <|> parseNixpkgsCustom <|> parseNoNixpkgs <|> pure NixpkgsFast
where
parseNixpkgsFast =
Opts.flag'
NixpkgsFast
( Opts.long "fast"
<> Opts.help "Use the latest nixpkgs cached at 'https://github.com/nmattia/niv/blob/master/data/nixpkgs.json'. This is the default."
)
parseNixpkgsLatest =
Opts.flag'
(NixpkgsCustom "master" (Nixpkgs "NixOS" "nixpkgs"))
( Opts.long "latest"
<> Opts.help "Pull the latest unstable nixpkgs from NixOS/nixpkgs."
)
parseNixpkgsCustom =
(flip NixpkgsCustom)
<$> ( Opts.option
customNixpkgsReader
( Opts.long "nixpkgs"
<> Opts.showDefault
<> Opts.help "Use a custom nixpkgs repository from GitHub."
<> Opts.metavar "OWNER/REPO"
)
)
<*> ( Opts.strOption
( Opts.long "nixpkgs-branch"
<> Opts.short 'b'
<> Opts.help "The nixpkgs branch when using --nixpkgs ...."
<> Opts.showDefault
)
)
parseNoNixpkgs =
Opts.flag'
NoNixpkgs
( Opts.long "no-nixpkgs"
<> Opts.help "Don't add a nixpkgs entry to sources.json."
)
customNixpkgsReader = Opts.maybeReader $ \(T.pack -> repo) -> case T.splitOn "/" repo of
[owner, reponame] -> Just (Nixpkgs owner reponame)
_ -> Nothing
cmdInit :: FetchNixpkgs -> NIO () cmdInit :: FetchNixpkgs -> NIO ()
cmdInit nixpkgs = do cmdInit nixpkgs = do
job "Initializing" $ do job "Initializing" $ do
@ -186,35 +198,9 @@ cmdInit nixpkgs = do
( pathNixSourcesJson fsj, ( pathNixSourcesJson fsj,
\path -> do \path -> do
createFile path initNixSourcesJsonContent createFile path initNixSourcesJsonContent
-- Imports @niv@ and @nixpkgs@
say "Importing 'niv' ..." -- Import nixpkgs, if necessary
cmdAdd initNixpkgs nixpkgs,
githubCmd
(PackageName "niv")
( specToFreeAttrs $
PackageSpec $
HMS.fromList
[ "owner" .= ("nmattia" :: T.Text),
"repo" .= ("niv" :: 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
githubCmd
(PackageName "nixpkgs")
( specToFreeAttrs $
PackageSpec $
HMS.fromList
[ "owner" .= owner,
"repo" .= repo,
"branch" .= branch
]
),
\path _content -> dontCreateFile path \path _content -> dontCreateFile path
) )
] ]
@ -249,6 +235,34 @@ cmdInit nixpkgs = do
dontCreateFile :: FilePath -> NIO () dontCreateFile :: FilePath -> NIO ()
dontCreateFile path = say $ "Not creating " <> path dontCreateFile path = say $ "Not creating " <> path
initNixpkgs :: FetchNixpkgs -> NIO ()
initNixpkgs nixpkgs =
case nixpkgs of
NoNixpkgs -> say "Not importing 'nixpkgs'."
NixpkgsFast -> do
say "Using known 'nixpkgs' ..."
packageSpec <- HTTP.getResponseBody <$> HTTP.httpJSON "https://raw.githubusercontent.com/nmattia/niv/master/data/nixpkgs.json"
cmdAdd
githubCmd
(PackageName "nixpkgs")
(specToLockedAttrs packageSpec)
pure ()
NixpkgsCustom branch nixpkgs' -> do
say "Importing 'nixpkgs' ..."
let (owner, repo) = case nixpkgs' of
Nixpkgs o r -> (o, r)
cmdAdd
githubCmd
(PackageName "nixpkgs")
( specToFreeAttrs $
PackageSpec $
HMS.fromList
[ "owner" .= owner,
"repo" .= repo,
"branch" .= branch
]
)
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
-- ADD -- ADD
------------------------------------------------------------------------------- -------------------------------------------------------------------------------

View File

@ -42,7 +42,7 @@ pkgs.runCommand "git-test"
pushd $nivdir > /dev/null pushd $nivdir > /dev/null
mkdir -p nix mkdir -p nix
echo "{}" > nix/sources.json echo "{}" > nix/sources.json
niv init niv init --latest
niv add git -n my-git-repo --repo file://$gitdir niv add git -n my-git-repo --repo file://$gitdir
nivrev=$(nix eval --json '(import ./nix/sources.nix).my-git-repo.rev' | jq -r) nivrev=$(nix eval --json '(import ./nix/sources.nix).my-git-repo.rev' | jq -r)
if [ ! "$gitrev" = "$nivrev" ]; then if [ ! "$gitrev" = "$nivrev" ]; then

View File

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