Skip to content

Commit 7fdb1bd

Browse files
committed
Add a flake-compat-based shell.nix to flake projects
Closes #1
1 parent dbb39c2 commit 7fdb1bd

File tree

12 files changed

+149
-12
lines changed

12 files changed

+149
-12
lines changed

.last-exported-commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Last exported commit from parent repo: 551f176e30505f6914aff5575a42ff299a9de75b
1+
Last exported commit from parent repo: 47b268f7306d5660250f84a5036faefd4b43cc7c

nix-bootstrap.cabal

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ cabal-version: 2.0
55
-- see: https://github.com/sol/hpack
66

77
name: nix-bootstrap
8-
version: 1.3.2.1
8+
version: 1.3.3.0
99
author: gchquser
1010
maintainer: [email protected]
1111
copyright: Crown Copyright
@@ -42,6 +42,7 @@ library
4242
Bootstrap.Data.Bootstrappable.Go.Modfile
4343
Bootstrap.Data.Bootstrappable.NixPreCommitHookConfig
4444
Bootstrap.Data.Bootstrappable.NixShell
45+
Bootstrap.Data.Bootstrappable.NixShellCompat
4546
Bootstrap.Data.Bootstrappable.Python.Requirements
4647
Bootstrap.Data.Bootstrappable.Readme
4748
Bootstrap.Data.Bootstrappable.VSCodeExtensions
@@ -174,6 +175,7 @@ test-suite nix-bootstrap-test
174175
Bootstrap.Data.Bootstrappable.GitPodYmlSpec
175176
Bootstrap.Data.Bootstrappable.Go.ModfileSpec
176177
Bootstrap.Data.Bootstrappable.NixPreCommitHookConfigSpec
178+
Bootstrap.Data.Bootstrappable.NixShellCompatSpec
177179
Bootstrap.Data.Bootstrappable.NixShellSpec
178180
Bootstrap.Data.Bootstrappable.ReadmeSpec
179181
Bootstrap.Data.Bootstrappable.VSCodeExtensionsSpec

package.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
name: nix-bootstrap
15-
version: 1.3.2.1
15+
version: 1.3.3.0
1616
author: gchquser
1717
maintainer: [email protected]
1818
copyright: Crown Copyright

src/Bootstrap.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import Bootstrap.Data.Bootstrappable.GitlabCIConfig (gitlabCIConfigFor)
4141
import Bootstrap.Data.Bootstrappable.Go.Modfile (goModfileFor)
4242
import Bootstrap.Data.Bootstrappable.NixPreCommitHookConfig (nixPreCommitHookConfigFor)
4343
import Bootstrap.Data.Bootstrappable.NixShell (nixShellFor)
44+
import Bootstrap.Data.Bootstrappable.NixShellCompat (nixShellCompatFor)
4445
import Bootstrap.Data.Bootstrappable.Python.Requirements (Requirements (Requirements))
4546
import Bootstrap.Data.Bootstrappable.Readme
4647
( Readme
@@ -450,6 +451,7 @@ makeBuildPlan MakeBuildPlanArgs {..} = do
450451
then Nothing
451452
else defaultNixFor mbpProjectName mbpProjectType
452453
)
454+
~: nixShellCompatFor mbpRunConfig
453455
~: nixPreCommitHookConfig
454456
~: gitlabCIConfigFor mbpContinuousIntegrationConfig mbpRunConfig mbpProjectType mbpPreCommitHooksConfig
455457
~: devContainerDockerComposeFor mbpDevContainerConfig mbpProjectName

src/Bootstrap/Data/Bootstrappable/FlakeNix.hs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,16 @@ instance IsNixExpr FlakeNix where
111111
|= ESet
112112
False
113113
( [ [nixbinding|nixpkgs-src.url = "github:NixOS/nixpkgs";|],
114+
[nixbinding|flake-compat = {
115+
flake = false;
116+
url = github:edolstra/flake-compat;
117+
};|],
114118
[nixbinding|flake-utils.url = "github:numtide/flake-utils";|]
115119
]
116120
<> [ [nixbinding|pre-commit-hooks-lib = {
117-
inputs.flake-utils.follows = "flake-utils";
118-
url = "github:cachix/pre-commit-hooks.nix";
119-
};|]
121+
inputs.flake-utils.follows = "flake-utils";
122+
url = "github:cachix/pre-commit-hooks.nix";
123+
};|]
120124
| usingHooks
121125
]
122126
<> [machNixFlakeInput | isPython]
@@ -128,6 +132,7 @@ instance IsNixExpr FlakeNix where
128132
[[nixident|mach-nix|] | isPython]
129133
<> [[nixident|pre-commit-hooks-lib|] | usingHooks]
130134
<> [[nixident|self|]]
135+
<> [[nixident|...|]]
131136
)
132137
)
133138
|: [nix|flake-utils.lib.eachSystem (with flake-utils.lib.system; [x86_64-linux aarch64-linux])|]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{-# LANGUAGE QuasiQuotes #-}
2+
{-# LANGUAGE TemplateHaskellQuotes #-}
3+
4+
-- | Copyright : (c) Crown Copyright GCHQ
5+
-- | Description : Compatibility for pre-flake tools to work with the bootstraped flake.nix
6+
module Bootstrap.Data.Bootstrappable.NixShellCompat
7+
( NixShellCompat,
8+
nixShellCompatFor,
9+
)
10+
where
11+
12+
import Bootstrap.Cli (RunConfig (RunConfig, rcUseFlakes))
13+
import Bootstrap.Data.Bootstrappable
14+
( Bootstrappable (bootstrapContent, bootstrapName, bootstrapReason),
15+
bootstrapContentNix,
16+
)
17+
import Bootstrap.Nix.Expr (IsNixExpr (toNixExpr), nix)
18+
19+
-- | A shell.nix file used to allow access to the devshell defined in flake.nix
20+
-- in a non-flake environment
21+
data NixShellCompat = NixShellCompat
22+
deriving stock (Eq, Show)
23+
24+
instance Bootstrappable NixShellCompat where
25+
bootstrapName = const "shell.nix"
26+
bootstrapReason NixShellCompat =
27+
"This enables you to use your development shell "
28+
<> "when Nix flakes aren't available."
29+
bootstrapContent = bootstrapContentNix
30+
31+
instance IsNixExpr NixShellCompat where
32+
toNixExpr NixShellCompat =
33+
[nix|(import (
34+
let
35+
lock = builtins.fromJSON (builtins.readFile ./flake.lock);
36+
in
37+
fetchTarball {
38+
url = "https://github.com/edolstra/flake-compat/archive/${
39+
lock.nodes.flake-compat.locked.rev
40+
}.tar.gz";
41+
sha256 = lock.nodes.flake-compat.locked.narHash;
42+
}) {src = ./.;}).shellNix|]
43+
44+
nixShellCompatFor :: RunConfig -> Maybe NixShellCompat
45+
nixShellCompatFor RunConfig {rcUseFlakes} =
46+
if rcUseFlakes
47+
then Just NixShellCompat
48+
else Nothing

src/Bootstrap/Nix/Expr.hs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,15 @@ newtype Identifier = Identifier {unIdentifier :: Text}
452452

453453
-- | Parses an `Identifier`
454454
parseIdentifier :: Parser Identifier
455-
parseIdentifier = label "identifier" do
456-
firstChar <- letterChar <|> char '_'
457-
rest <- many $ alphaNumChar <|> char '_' <|> char '-'
458-
pure . Identifier . toText $ firstChar : rest
455+
parseIdentifier =
456+
label "identifier" . fmap Identifier $
457+
choice
458+
[ string "...",
459+
do
460+
firstChar <- letterChar <|> char '_'
461+
rest <- many $ alphaNumChar <|> char '_' <|> char '-'
462+
pure . toText $ firstChar : rest
463+
]
459464

460465
-- | Parses a list of `Expr`s
461466
parseList :: Parser [Expr]
@@ -678,7 +683,11 @@ isCorrectlyScoped' scope = \case
678683
let additionalScope = case args of FAOne i -> [i]; FASet is -> toList is
679684
in isCorrectlyScoped' (scope <> additionalScope) e
680685
EGrouping e -> isCorrectlyScoped' scope e
681-
EIdent i -> if i `elem` scope || i == Identifier "builtins" then pass else Left (one i)
686+
EIdent i
687+
| i `elem` scope -> pass
688+
| i == Identifier "builtins" -> pass
689+
| i == Identifier "fetchTarball" -> pass
690+
| otherwise -> Left (one i)
682691
EImport -> pass
683692
ELetIn (toList -> bindings) e ->
684693
mergeScopeResults

test/Bootstrap/Data/Bootstrappable/FlakeNixSpec.hs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,17 @@ spec = describe "flake.nix rendering" do
3232
description = "Development infrastructure for test-project";
3333
inputs = {
3434
nixpkgs-src.url = "github:NixOS/nixpkgs";
35+
flake-compat = {
36+
flake = false;
37+
url = github:edolstra/flake-compat;
38+
};
3539
flake-utils.url = "github:numtide/flake-utils";
3640
};
3741
outputs = {
3842
nixpkgs-src,
3943
flake-utils,
4044
self,
45+
...
4146
}:
4247
flake-utils.lib.eachSystem (with flake-utils.lib.system; [x86_64-linux aarch64-linux]) (system: let
4348
nixpkgs = nixpkgs-src.legacyPackages.${system};
@@ -62,12 +67,17 @@ spec = describe "flake.nix rendering" do
6267
description = "Development infrastructure for test-project";
6368
inputs = {
6469
nixpkgs-src.url = "github:NixOS/nixpkgs";
70+
flake-compat = {
71+
flake = false;
72+
url = github:edolstra/flake-compat;
73+
};
6574
flake-utils.url = "github:numtide/flake-utils";
6675
};
6776
outputs = {
6877
nixpkgs-src,
6978
flake-utils,
7079
self,
80+
...
7181
}:
7282
flake-utils.lib.eachSystem (with flake-utils.lib.system; [x86_64-linux aarch64-linux]) (system: let
7383
nixpkgs = nixpkgs-src.legacyPackages.${system};
@@ -101,6 +111,10 @@ spec = describe "flake.nix rendering" do
101111
description = "Development infrastructure for test-project";
102112
inputs = {
103113
nixpkgs-src.url = "github:NixOS/nixpkgs";
114+
flake-compat = {
115+
flake = false;
116+
url = github:edolstra/flake-compat;
117+
};
104118
flake-utils.url = "github:numtide/flake-utils";
105119
pre-commit-hooks-lib = {
106120
inputs.flake-utils.follows = "flake-utils";
@@ -112,6 +126,7 @@ spec = describe "flake.nix rendering" do
112126
flake-utils,
113127
pre-commit-hooks-lib,
114128
self,
129+
...
115130
}:
116131
flake-utils.lib.eachSystem (with flake-utils.lib.system; [x86_64-linux aarch64-linux]) (system: let
117132
nixpkgs = nixpkgs-src.legacyPackages.${system};
@@ -157,6 +172,10 @@ spec = describe "flake.nix rendering" do
157172
description = "Development infrastructure for test-project";
158173
inputs = {
159174
nixpkgs-src.url = "github:NixOS/nixpkgs";
175+
flake-compat = {
176+
flake = false;
177+
url = github:edolstra/flake-compat;
178+
};
160179
flake-utils.url = "github:numtide/flake-utils";
161180
pre-commit-hooks-lib = {
162181
inputs.flake-utils.follows = "flake-utils";
@@ -168,6 +187,7 @@ spec = describe "flake.nix rendering" do
168187
flake-utils,
169188
pre-commit-hooks-lib,
170189
self,
190+
...
171191
}:
172192
flake-utils.lib.eachSystem (with flake-utils.lib.system; [x86_64-linux aarch64-linux]) (system: let
173193
nixpkgs = nixpkgs-src.legacyPackages.${system};
@@ -203,6 +223,10 @@ spec = describe "flake.nix rendering" do
203223
description = "Development infrastructure for test-project";
204224
inputs = {
205225
nixpkgs-src.url = "github:NixOS/nixpkgs";
226+
flake-compat = {
227+
flake = false;
228+
url = github:edolstra/flake-compat;
229+
};
206230
flake-utils.url = "github:numtide/flake-utils";
207231
mach-nix.url = "github:DavHau/mach-nix?ref=3.5.0";
208232
};
@@ -211,6 +235,7 @@ spec = describe "flake.nix rendering" do
211235
flake-utils,
212236
mach-nix,
213237
self,
238+
...
214239
}:
215240
flake-utils.lib.eachSystem (with flake-utils.lib.system; [x86_64-linux aarch64-linux]) (system: let
216241
nixpkgs = nixpkgs-src.legacyPackages.${system};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{-# LANGUAGE QuasiQuotes #-}
2+
3+
-- | Copyright : (c) Crown Copyright GCHQ
4+
module Bootstrap.Data.Bootstrappable.NixShellCompatSpec (spec) where
5+
6+
import Bootstrap.Data.Bootstrappable (Bootstrappable (bootstrapContent))
7+
import Bootstrap.Data.Bootstrappable.NixShellCompat
8+
( nixShellCompatFor,
9+
)
10+
import Test.Hspec (Spec, describe, it)
11+
import Test.Hspec.Expectations.Pretty (shouldBe)
12+
import Test.Util.RunConfig (rcDefault, rcWithFlakes)
13+
import Text.RawString.QQ (r)
14+
15+
spec :: Spec
16+
spec = describe "shell.nix (compat) rendering" do
17+
it "isn't created for non-flake projects" (nixShellCompatFor rcDefault `shouldBe` Nothing)
18+
it "renders correctly" do
19+
bootstrapContent (nixShellCompatFor rcWithFlakes)
20+
>>= ( `shouldBe`
21+
Right
22+
[r|(import (let
23+
lock = builtins.fromJSON (builtins.readFile ./flake.lock);
24+
in
25+
fetchTarball {
26+
url = "https://github.com/edolstra/flake-compat/archive/${
27+
lock.nodes.flake-compat.locked.rev
28+
}.tar.gz";
29+
sha256 = lock.nodes.flake-compat.locked.narHash;
30+
}) {
31+
src = ./.;
32+
})
33+
.shellNix
34+
|]
35+
)

test/Bootstrap/Data/BuildPlanSpec.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import Bootstrap.Data.Bootstrappable.Gitignore (gitignoreFor)
1515
import Bootstrap.Data.Bootstrappable.GitlabCIConfig (gitlabCIConfigFor)
1616
import Bootstrap.Data.Bootstrappable.NixPreCommitHookConfig (nixPreCommitHookConfigFor)
1717
import Bootstrap.Data.Bootstrappable.NixShell (nixShellFor)
18+
import Bootstrap.Data.Bootstrappable.NixShellCompat (nixShellCompatFor)
1819
import Bootstrap.Data.Bootstrappable.Readme (Readme (Readme))
1920
import Bootstrap.Data.Bootstrappable.VSCodeExtensions (vsCodeExtensionsFileFor)
2021
import Bootstrap.Data.Bootstrappable.VSCodeSettings (vsCodeSettingsFor)
@@ -59,6 +60,7 @@ spec = describe "toReasonTree" do
5960
~: flakeNixFor rcWithFlakes projectName projectType preCommitHooksConfig (Just nixPreCommitHookConfig) buildNix
6061
~: defaultNixFor projectName projectType
6162
~: nixShellFor rcDefault projectType preCommitHooksConfig (Just nixPreCommitHookConfig)
63+
~: nixShellCompatFor rcWithFlakes
6264
~: gitignoreFor rcDefault projectType preCommitHooksConfig
6365
~: Readme projectName projectType devContainerConfig Nothing False
6466
~: nixPreCommitHookConfig
@@ -101,5 +103,8 @@ spec = describe "toReasonTree" do
101103
Node "sources.nix - This is the interface between nix and the dependencies listed in sources.json." []
102104
],
103105
Node "README.md - This helpfully explains to you what each file (including itself) does!" [],
104-
Node "shell.nix - This configures what tools are available in your development environment and links in the pre-commit hooks." []
106+
Node
107+
"shell.nix - This configures what tools are available in your development environment and links in the pre-commit hooks."
108+
[],
109+
Node "shell.nix - This enables you to use your development shell when Nix flakes aren't available." []
105110
]

0 commit comments

Comments
 (0)