Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Commit

Permalink
feat: use flakehub for installs, self-manage fleek, autogc (#282)
Browse files Browse the repository at this point in the history
* feat: use flakehub for installs, self-manage fleek, autogc

* fix: doc comments

* fix: fmt

* fix: fmt

* fix: discard error

* fix: readme
  • Loading branch information
bketelsen authored Sep 20, 2023
1 parent 2815824 commit e367f50
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 62 deletions.
47 changes: 3 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Fleek - "Home as Code" for Humans

![FlakeHub](https://img.shields.io/endpoint?url=https://flakehub.com/f/ublue-os/fleek/badge)

[![FlakeHub](https://img.shields.io/endpoint?url=https://flakehub.com/f/ublue-os/fleek/badge)](https://flakehub.com/flake/ublue-os/fleek)



Expand Down Expand Up @@ -44,49 +45,7 @@ Fleek is a user-friendly wrapper around Nix and Nix Home Manager, but the friend

## Getting Started

You need `nix`. We love the [Determinate Systems Installer](https://zero-to-nix.com/), but any `nix` installer is good. If you're on Fedora Silverblue, [this script](https://github.com/dnkmmr69420/nix-installer-scripts/blob/main/installer-scripts/silverblue-nix-installer.sh) is the good stuff.

After Nix is installed you need to enable [flakes and the nix command](https://nixos.wiki/wiki/Flakes). It can be as simple as this:

```shell
mkdir -p ~/.config/nix
echo "experimental-features = nix-command flakes" >> ~/.config/nix/nix.conf
```

Install `fleek`:

```shell
curl -fsSL https://getfleek.dev/installer | bash
```

Run `fleek init`:

```shell
fleek init
```


This will create your configuration file and symlink it to `$HOME/.fleek.yml`. Open it with your favorite editor and take a look.

Make any changes to the `~/.fleek.yml` file you want... we recommend Bling Level `high` for the best experience.

Now let's apply your configuration:

```shell
fleek apply
```
It will take a bit to download and install everything, but when it's done you should see something like this:

```shell
... more text above this ...
Activating onFilesChange
Activating reloadSystemd
[✓] Operation completed successfully
```

*What happened here?* We just installed Nix Home Manager, configured it with your preferences, and applied it to your system.

You may need to close and re-open your terminal or even log out to see the changes.
See [the installation instructions](https://getfleek.dev/docs/installation).

## ~/.fleek.yml

Expand Down
15 changes: 15 additions & 0 deletions internal/flake/flake.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,21 @@ func (f *Flake) writeUser(sys fleek.System, user fleek.User, template string, fo

return nil
}

func (f *Flake) WriteTemplates() error {

writeCmdLine := []string{"run", ".#fleek", "--", "write"}
err := f.runNix(nixbin, writeCmdLine)
if err != nil {
return err
}
err = f.mayCommit("fleek: update templates")

if err != nil {
return err
}
return nil
}
func (f *Flake) Apply() error {
fin.Info.Println(f.app.Trans("flake.apply"))

Expand Down
13 changes: 10 additions & 3 deletions internal/flake/templates/flake.nix.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
nixpkgs.url = "github:nixos/nixpkgs/{{ .Config.Tracks }}";

# Home manager
home-manager.url = "github:nix-community/home-manager";
home-manager.url = "https://flakehub.com/f/nix-community/home-manager/0.1.tar.gz";
home-manager.inputs.nixpkgs.follows = "nixpkgs";

# Fleek
fleek.url = "github:ublue-os/fleek";
fleek.url = "https://flakehub.com/f/ublue-os/fleek/*.tar.gz";

# Overlays
{{ range $index, $element := .Config.Overlays }}
Expand All @@ -22,7 +22,9 @@
};

outputs = { self, nixpkgs, home-manager, fleek, ... }@inputs: {

{{ range .Config.Systems }}
packages.{{ .Arch }}-{{ .OS }}.fleek = fleek.packages.{{ .Arch }}-{{ .OS }}.default;
{{ end }}
# Available through 'home-manager --flake .#your-username@your-hostname'
{{ $overlays := .Config.Overlays }}
homeConfigurations = {
Expand All @@ -41,6 +43,11 @@
./{{.Hostname}}/{{.Username}}.nix
./{{.Hostname}}/custom.nix
# self-manage fleek
{
home.packages = [
fleek.packages.{{ .Arch }}-{{ .OS }}.default
];
}
({
nixpkgs.overlays = [{{ range $index, $element := $overlays }}inputs.{{$index}}.overlay {{ end }}];
})
Expand Down
15 changes: 15 additions & 0 deletions internal/fleek/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type Config struct {
Users []*User `yaml:",flow"`
Track string `yaml:"track"`
AllowBroken bool `yaml:"allow_broken"`
AutoGC bool `yaml:"auto_gc"`
}

func Levels() []string {
Expand Down Expand Up @@ -120,6 +121,19 @@ func NewSystem() (*System, error) {
Username: user,
}, nil
}

// CollectGarbage runs nix-collect-garbage
func CollectGarbage() error {

command := exec.Command("nix-collect-garbage", "-d")
command.Stdin = os.Stdin
command.Stderr = os.Stderr
command.Stdout = os.Stdout
command.Env = os.Environ()

return command.Run()

}
func NewUser() (*User, error) {
fin.Info.Println("Enter User Details for Git Configuration:")
user := &User{}
Expand Down Expand Up @@ -460,6 +474,7 @@ func (c *Config) WriteInitialConfig(force bool, symlink bool) error {
return err
}
c.Unfree = true
c.AutoGC = true
c.Name = "Fleek Configuration"
c.Packages = []string{
"helix",
Expand Down
11 changes: 10 additions & 1 deletion internal/fleekcli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ func RootCmd() *cobra.Command {
fin.Debug.Printfln("git autopush: %v", cfg.Git.AutoPush)
fin.Debug.Printfln("git autocommit: %v", cfg.Git.AutoCommit)
fin.Debug.Printfln("git autopull: %v", cfg.Git.AutoPull)
fin.Debug.Printfln("auto gc: %v", cfg.AutoGC)

if cfg.AutoGC {
fin.Info.Println("Running nix-collect-garbage")
// we don't care too much if there's an error here
_ = fleek.CollectGarbage()
}

},
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -152,6 +159,8 @@ func RootCmd() *cobra.Command {

infoCmd := InfoCommand()
infoCmd.GroupID = packageGroup.ID
writeCmd := WriteCommand()
writeCmd.GroupID = fleekGroup.ID
manCmd := ManCommand()

docsCmd := genDocsCmd()
Expand All @@ -171,7 +180,7 @@ func RootCmd() *cobra.Command {
command.AddCommand(searchCmd)
command.AddCommand(infoCmd)
command.AddCommand(generateCmd)

command.AddCommand(writeCmd)
command.AddCommand(VersionCmd())

command.PersistentFlags().BoolVarP(
Expand Down
6 changes: 6 additions & 0 deletions internal/fleekcli/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ func update(cmd *cobra.Command) error {
if err := fl.Update(); err != nil {
return err
}
// We just updated the flake lock, which might pull a new
// version of fleek in. Update the system templates to
// get new fixes without having to update/apply twice
if err := fl.WriteTemplates(); err != nil {
return err
}
if cmd.Flag(app.Trans("update.applyFlag")).Changed {
if err := fl.Apply(); err != nil {
return err
Expand Down
14 changes: 0 additions & 14 deletions internal/fleekcli/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/spf13/cobra"
"github.com/ublue-os/fleek/internal/build"
"github.com/ublue-os/fleek/internal/vercheck"
)

type versionFlags struct {
Expand All @@ -27,7 +26,6 @@ func VersionCmd() *cobra.Command {
command.Flags().BoolVarP(&flags.verbose, app.Trans("version.flagVerbose"), "v", false, // value
app.Trans("version.flagVerboseDescription"),
)
command.AddCommand(selfUpdateCmd())

return command
}
Expand All @@ -47,18 +45,6 @@ func versionCmdFunc(cmd *cobra.Command, _ []string, flags versionFlags) error {
}
return nil
}
func selfUpdateCmd() *cobra.Command {
command := &cobra.Command{
Use: "update",
Short: "Update fleek launcher and binary",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return vercheck.SelfUpdate(cmd.OutOrStdout(), cmd.OutOrStderr())
},
}

return command
}

type versionInfo struct {
Version string
Expand Down
48 changes: 48 additions & 0 deletions internal/fleekcli/write.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package fleekcli

import (
"github.com/spf13/cobra"
"github.com/ublue-os/fleek/fin"
"github.com/ublue-os/fleek/internal/flake"
)

// WriteCommand is an internal hidden command that
// gets run after an update.
func WriteCommand() *cobra.Command {
command := &cobra.Command{
Hidden: true,
Use: app.Trans("write.use"),
Short: app.Trans("write.short"),
Long: app.Trans("write.long"),
RunE: func(cmd *cobra.Command, args []string) error {
return write(cmd)
},
}

return command
}

// writeCmd represents the write command
func write(cmd *cobra.Command) error {
fin.Description.Println(cmd.Short)
err := mustConfig()
if err != nil {
return err
}
fl, err := flake.Load(cfg, app)
if err != nil {
return err
}

err = fl.Write("flake update", true, false)
if err != nil {
fin.Debug.Printfln("flake write error: %s", err)
return err
}

fin.Success.Println(app.Trans("write.done"))
return nil
}
7 changes: 7 additions & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,13 @@ info:
notFound: "That program or package is not part of fleek's bling set."
aliases: "Shell Aliases"
description: "Description"
write:
use: "write"
long: "Apply system templates to existing flake"
example: |
fleek write
short: "Apply system templates to existing flake"
done: "Flake templates written."
flake:
noConfig: "No configuration files found. Try `fleek init`."
configLoaded: "Configuration loaded"
Expand Down

0 comments on commit e367f50

Please sign in to comment.