Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to configure niv directory path #133

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -52,6 +52,7 @@ The following environment variables are read by `niv`:

| Name | Note |
| --------------- | ---- |
| NIV_DIR | Directory where sources.nix and sources.json will be located, defaults to "nix". |
| GITHUB_TOKEN | When set, the value is used to authenticate GitHub API requests. |
| GITHUB_HOST | The GitHub host to use when fetching packages. Port may be appended here. |
| GITHUB_API_HOST | The host used when performing GitHub API requests. Use `GITHUB_API_PORT` for specifying the port. |
Expand Down
1 change: 1 addition & 0 deletions README.tpl.md
Expand Up @@ -52,6 +52,7 @@ The following environment variables are read by `niv`:

| Name | Note |
| --------------- | ---- |
| NIV_DIR | Directory where sources.nix and sources.json will be located, defaults to "nix". |
| GITHUB_TOKEN | When set, the value is used to authenticate GitHub API requests. |
| GITHUB_HOST | The GitHub host to use when fetching packages. Port may be appended here. |
| GITHUB_API_HOST | The host used when performing GitHub API requests. Use `GITHUB_API_PORT` for specifying the port. |
Expand Down
10 changes: 8 additions & 2 deletions src/Niv/Cli.hs
Expand Up @@ -20,7 +20,9 @@ import Data.String.QQ (s)
import Niv.Logger
import Niv.GitHub
import Niv.Update
import System.Environment (lookupEnv)
import System.Exit (ExitCode(ExitSuccess))
import System.IO.Unsafe (unsafePerformIO)
import System.FilePath ((</>), takeDirectory)
import System.Process (readProcessWithExitCode)
import UnliftIO
Expand Down Expand Up @@ -579,15 +581,19 @@ warnIfOutdated = do

-- | @nix/sources.nix@
pathNixSourcesNix :: FilePath
pathNixSourcesNix = "nix" </> "sources.nix"
pathNixSourcesNix = unsafePerformIO $ do
nivPath <- lookupEnv "NIV_DIR"
pure $ maybe "nix" id nivPath </> "sources.nix"

-- | Glue code between nix and sources.json
initNixSourcesNixContent :: B.ByteString
initNixSourcesNixContent = $(embedFile "nix/sources.nix")

-- | @nix/sources.json"
pathNixSourcesJson :: FilePath
pathNixSourcesJson = "nix" </> "sources.json"
pathNixSourcesJson = unsafePerformIO $ do
nivPath <- lookupEnv "NIV_DIR"
pure $ maybe "nix" id nivPath </> "sources.json"

-- | Empty JSON map
initNixSourcesJsonContent :: B.ByteString
Expand Down
9 changes: 9 additions & 0 deletions tests/default.nix
Expand Up @@ -122,4 +122,13 @@ in pkgs.runCommand "test"
cat nix/sources.json | jq -e '.foo | .url == "localhost:3333/foo-v1"'
echo "*** ok."
cp nix/sources.json $out

echo -e "\n*** NIV_DIR=nix2 niv init"
NIV_DIR=nix2 niv init
diff -h ${./expected/niv-init.json} nix2/sources.json || \
(echo "Mismatched sources.json"; \
echo "Reference: tests/expected/niv-init.json"; \
exit 1)

echo "*** ok."
''