1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use std::{
    fs::Permissions,
    os::unix::prelude::PermissionsExt,
    path::{Path, PathBuf},
};

use tracing::{span, Span};
use walkdir::WalkDir;

use crate::action::{
    Action, ActionDescription, ActionError, ActionErrorKind, ActionTag, StatefulAction,
};

pub(crate) const DEST: &str = "/nix/";

/**
Move an unpacked Nix at `src` to `/nix`
*/
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone)]
pub struct MoveUnpackedNix {
    unpacked_path: PathBuf,
}

impl MoveUnpackedNix {
    #[tracing::instrument(level = "debug", skip_all)]
    pub async fn plan(unpacked_path: PathBuf) -> Result<StatefulAction<Self>, ActionError> {
        // Note: Do NOT try to check for the src/dest since the installer creates those
        Ok(Self { unpacked_path }.into())
    }
}

#[async_trait::async_trait]
#[typetag::serde(name = "mount_unpacked_nix")]
impl Action for MoveUnpackedNix {
    fn action_tag() -> ActionTag {
        ActionTag("move_unpacked_nix")
    }
    fn tracing_synopsis(&self) -> String {
        "Move the downloaded Nix into `/nix`".to_string()
    }

    fn tracing_span(&self) -> Span {
        span!(
            tracing::Level::DEBUG,
            "mount_unpacked_nix",
            src = tracing::field::display(self.unpacked_path.display()),
            dest = DEST,
        )
    }

    fn execute_description(&self) -> Vec<ActionDescription> {
        vec![ActionDescription::new(
            format!("Move the downloaded Nix into `/nix`"),
            vec![format!(
                "Nix is being downloaded to `{}` and should be in `/nix`",
                self.unpacked_path.display(),
            )],
        )]
    }

    #[tracing::instrument(level = "debug", skip_all)]
    async fn execute(&mut self) -> Result<(), ActionError> {
        let Self { unpacked_path } = self;

        // This is the `nix-$VERSION` folder which unpacks from the tarball, not a nix derivation
        let found_nix_paths = glob::glob(&format!("{}/nix-*", unpacked_path.display()))
            .map_err(|e| Self::error(MoveUnpackedNixError::from(e)))?
            .collect::<Result<Vec<_>, _>>()
            .map_err(|e| Self::error(MoveUnpackedNixError::from(e)))?;
        if found_nix_paths.len() != 1 {
            return Err(Self::error(ActionErrorKind::MalformedBinaryTarball));
        }
        let found_nix_path = found_nix_paths.into_iter().next().unwrap();
        let src_store = found_nix_path.join("store");
        let mut src_store_listing = tokio::fs::read_dir(src_store.clone())
            .await
            .map_err(|e| ActionErrorKind::ReadDir(src_store.clone(), e))
            .map_err(Self::error)?;
        let dest_store = Path::new(DEST).join("store");
        if dest_store.exists() {
            if !dest_store.is_dir() {
                return Err(Self::error(ActionErrorKind::PathWasNotDirectory(
                    dest_store.clone(),
                )))?;
            }
        } else {
            tokio::fs::create_dir(&dest_store)
                .await
                .map_err(|e| ActionErrorKind::CreateDirectory(dest_store.clone(), e))
                .map_err(Self::error)?;
        }

        while let Some(entry) = src_store_listing
            .next_entry()
            .await
            .map_err(|e| ActionErrorKind::ReadDir(src_store.clone(), e))
            .map_err(Self::error)?
        {
            let entry_dest = dest_store.join(entry.file_name());
            if entry_dest.exists() {
                tracing::trace!(src = %entry.path().display(), dest = %entry_dest.display(), "Removing already existing package");
                tokio::fs::remove_dir_all(&entry_dest)
                    .await
                    .map_err(|e| ActionErrorKind::Remove(entry_dest.clone(), e))
                    .map_err(Self::error)?;
            }
            tracing::trace!(src = %entry.path().display(), dest = %entry_dest.display(), "Renaming");
            tokio::fs::rename(&entry.path(), &entry_dest)
                .await
                .map_err(|e| {
                    ActionErrorKind::Rename(entry.path().clone(), entry_dest.to_owned(), e)
                })
                .map_err(Self::error)?;

            let perms: Permissions = PermissionsExt::from_mode(0o555);
            for entry_item in WalkDir::new(&entry_dest)
                .into_iter()
                .filter_map(Result::ok)
                .filter(|e| !e.file_type().is_symlink())
            {
                tokio::fs::set_permissions(&entry_item.path(), perms.clone())
                    .await
                    .map_err(|e| {
                        ActionErrorKind::SetPermissions(
                            perms.mode(),
                            entry_item.path().to_owned(),
                            e,
                        )
                    })
                    .map_err(Self::error)?;
            }

            // Leave a back link where we copied from since later we may need to know which packages we actually transferred
            // eg, know which `nix` version we installed when curing a user with several versions installed
            tokio::fs::symlink(&entry_dest, entry.path())
                .await
                .map_err(|e| {
                    ActionErrorKind::Symlink(entry_dest.to_owned(), entry.path().clone(), e)
                })
                .map_err(Self::error)?;
        }

        Ok(())
    }

    fn revert_description(&self) -> Vec<ActionDescription> {
        vec![/* Deliberately empty -- this is a noop */]
    }

    #[tracing::instrument(level = "debug", skip_all)]
    async fn revert(&mut self) -> Result<(), ActionError> {
        // Noop
        Ok(())
    }
}

#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
pub enum MoveUnpackedNixError {
    #[error("Glob pattern error")]
    GlobPatternError(
        #[from]
        #[source]
        glob::PatternError,
    ),
    #[error("Glob globbing error")]
    GlobGlobError(
        #[from]
        #[source]
        glob::GlobError,
    ),
}

impl Into<ActionErrorKind> for MoveUnpackedNixError {
    fn into(self) -> ActionErrorKind {
        ActionErrorKind::Custom(Box::new(self))
    }
}